By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,447 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Combo Box to populate Data Grid

P: n/a

I'm open for any suggestions on how to better program this. I want the user
to select a license from a combo box, cboPrivilege and then the user will
click the add button, then a record will be added to the data grid.

My current code is coming up with errors, dt and drv are unknown in the else
part of the if statement.
if (this.dgPrivileges.DataSource == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Code", typeof(string));
dt.Columns.Add("Amount", typeof(float));
this.dgPrivileges.DataSource = dt;

DataRowView
drv=this.cboPrivilege.SelectedItem as DataRowView;
dt.Rows.Add(new
object[]{drv["LICENSE_CODE"].ToString(), drv["GROSS_FEE_AMOUNT"].ToString()});
cboPrivilege.Focus();
}
else
{
dt.Rows.Add(new
object[]{drv["LICENSE_CODE"].ToString(), drv["GROSS_FEE_AMOUNT"].ToString()});
}

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Cadel,

Welcome to MSDN newsgroup.
From your descrption, you bind some option datas in a ComboBox (from a
datasource...), and add a Button, when user click that button, you want to
add a new record to another DataTable which is bind to a DataGrid, and the
data is coming from the datasource of the ComboBox control, yes?

IMO, I think you can first put the DataSource initializing code for both
the ComboBox and DataGrid into the Form's initializaing time, e.g , in the
Form_Load event. e.g:

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dtOptions = new DataTable("options");
dtOptions.Columns.Add("name",typeof(string));
dtOptions.Columns.Add("desc", typeof(string));

for(int i=0; i<10; i++)
{
DataRow dr = dtOptions.NewRow();
dr[0] ="Name_" + i;
dr[1] = "Desc_" + i;

dtOptions.Rows.Add(dr);
}

cbOptions.DataSource =dtOptions;
cbOptions.DisplayMember = "name";
cbOptions.ValueMember = "desc";
DataTable dtItems = dtOptions.Clone();
dgItems.DataSource = dtItems;

}

And when we need to add new record into the DataGrid, we are actually
adding new record into the DataGrid's Binding DataSource(the underlying
datatable). So in your Add button's click event, our code is something like:
private void btnAdd_Click(object sender, System.EventArgs e)
{

DataRowView drv = cbOptions.SelectedItem as DataRowView;

string name = (string)drv[0];
string desc = (string)drv[1];

DataTable dt = dgItems.DataSource as DataTable;

DataRow dr = dt.NewRow();

dr[0] = name;
dr[1] = desc;

dt.Rows.Add(dr);
}
Note that when adding new item into DataTable, we need to call
DataTable.NewRow to create a new DataRow instance, fill in the datas and
add it into the Rows Colleciton.

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| Thread-Topic: Combo Box to populate Data Grid
| thread-index: AcW/rmno1XNeCtNjQQOB1PkA3rAteg==
| X-WBNR-Posting-Host: 204.87.106.66
| From: =?Utf-8?B?TWlrZSBM?= <Ca***@nospam.nospam>
| Subject: Combo Box to populate Data Grid
| Date: Thu, 22 Sep 2005 12:47:07 -0700
| Lines: 30
| Message-ID: <C3**********************************@microsoft.co m>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:124211
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
|
| I'm open for any suggestions on how to better program this. I want the
user
| to select a license from a combo box, cboPrivilege and then the user will
| click the add button, then a record will be added to the data grid.
|
| My current code is coming up with errors, dt and drv are unknown in the
else
| part of the if statement.
|
|
| if (this.dgPrivileges.DataSource == null)
| {
| DataTable dt = new DataTable();
| dt.Columns.Add("Code", typeof(string));
| dt.Columns.Add("Amount", typeof(float));
| this.dgPrivileges.DataSource = dt;
|
| DataRowView
| drv=this.cboPrivilege.SelectedItem as DataRowView;
| dt.Rows.Add(new
| object[]{drv["LICENSE_CODE"].ToString(),
drv["GROSS_FEE_AMOUNT"].ToString()});
|
|
| cboPrivilege.Focus();
| }
| else
| {
| dt.Rows.Add(new
| object[]{drv["LICENSE_CODE"].ToString(),
drv["GROSS_FEE_AMOUNT"].ToString()});
| }
|
|

Nov 17 '05 #2

P: n/a
Kav
Hi Mike,

Move creation of the DataTable dt to just before the if statement, currently
you are creating a DataTable object with the scope of the if statement. Then
the reference to dt is removed at the end of the if statement.

You also need to create a DataRowView within the scope of else as the else
part cannot know about objects created within the scope of its relevant if.

HTH
Rich.
"Mike L" <Ca***@nospam.nospam> wrote in message
news:C3**********************************@microsof t.com...

I'm open for any suggestions on how to better program this. I want the
user
to select a license from a combo box, cboPrivilege and then the user will
click the add button, then a record will be added to the data grid.

My current code is coming up with errors, dt and drv are unknown in the
else
part of the if statement.
if (this.dgPrivileges.DataSource == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Code", typeof(string));
dt.Columns.Add("Amount", typeof(float));
this.dgPrivileges.DataSource = dt;

DataRowView
drv=this.cboPrivilege.SelectedItem as DataRowView;
dt.Rows.Add(new
object[]{drv["LICENSE_CODE"].ToString(),
drv["GROSS_FEE_AMOUNT"].ToString()});
cboPrivilege.Focus();
}
else
{
dt.Rows.Add(new
object[]{drv["LICENSE_CODE"].ToString(),
drv["GROSS_FEE_AMOUNT"].ToString()});
}

Nov 17 '05 #3

P: n/a
It worked. Thank you.

"Steven Cheng[MSFT]" wrote:
Hi Cadel,

Welcome to MSDN newsgroup.
From your descrption, you bind some option datas in a ComboBox (from a
datasource...), and add a Button, when user click that button, you want to
add a new record to another DataTable which is bind to a DataGrid, and the
data is coming from the datasource of the ComboBox control, yes?

IMO, I think you can first put the DataSource initializing code for both
the ComboBox and DataGrid into the Form's initializaing time, e.g , in the
Form_Load event. e.g:

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dtOptions = new DataTable("options");
dtOptions.Columns.Add("name",typeof(string));
dtOptions.Columns.Add("desc", typeof(string));

for(int i=0; i<10; i++)
{
DataRow dr = dtOptions.NewRow();
dr[0] ="Name_" + i;
dr[1] = "Desc_" + i;

dtOptions.Rows.Add(dr);
}

cbOptions.DataSource =dtOptions;
cbOptions.DisplayMember = "name";
cbOptions.ValueMember = "desc";
DataTable dtItems = dtOptions.Clone();
dgItems.DataSource = dtItems;

}

And when we need to add new record into the DataGrid, we are actually
adding new record into the DataGrid's Binding DataSource(the underlying
datatable). So in your Add button's click event, our code is something like:
private void btnAdd_Click(object sender, System.EventArgs e)
{

DataRowView drv = cbOptions.SelectedItem as DataRowView;

string name = (string)drv[0];
string desc = (string)drv[1];

DataTable dt = dgItems.DataSource as DataTable;

DataRow dr = dt.NewRow();

dr[0] = name;
dr[1] = desc;

dt.Rows.Add(dr);
}
Note that when adding new item into DataTable, we need to call
DataTable.NewRow to create a new DataRow instance, fill in the datas and
add it into the Rows Colleciton.

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| Thread-Topic: Combo Box to populate Data Grid
| thread-index: AcW/rmno1XNeCtNjQQOB1PkA3rAteg==
| X-WBNR-Posting-Host: 204.87.106.66
| From: =?Utf-8?B?TWlrZSBM?= <Ca***@nospam.nospam>
| Subject: Combo Box to populate Data Grid
| Date: Thu, 22 Sep 2005 12:47:07 -0700
| Lines: 30
| Message-ID: <C3**********************************@microsoft.co m>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:124211
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
|
| I'm open for any suggestions on how to better program this. I want the
user
| to select a license from a combo box, cboPrivilege and then the user will
| click the add button, then a record will be added to the data grid.
|
| My current code is coming up with errors, dt and drv are unknown in the
else
| part of the if statement.
|
|
| if (this.dgPrivileges.DataSource == null)
| {
| DataTable dt = new DataTable();
| dt.Columns.Add("Code", typeof(string));
| dt.Columns.Add("Amount", typeof(float));
| this.dgPrivileges.DataSource = dt;
|
| DataRowView
| drv=this.cboPrivilege.SelectedItem as DataRowView;
| dt.Rows.Add(new
| object[]{drv["LICENSE_CODE"].ToString(),
drv["GROSS_FEE_AMOUNT"].ToString()});
|
|
| cboPrivilege.Focus();
| }
| else
| {
| dt.Rows.Add(new
| object[]{drv["LICENSE_CODE"].ToString(),
drv["GROSS_FEE_AMOUNT"].ToString()});
| }
|
|

Nov 17 '05 #4

P: n/a
You're welcome :-)

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| Thread-Topic: Combo Box to populate Data Grid
| thread-index: AcXAfz/4T06HiS6eT+ioay1S6rft3g==
| X-WBNR-Posting-Host: 204.87.106.66
| From: =?Utf-8?B?TWlrZSBM?= <Ca***@nospam.nospam>
| References: <C3**********************************@microsoft.co m>
<9A*************@TK2MSFTNGXA01.phx.gbl>
| Subject: RE: Combo Box to populate Data Grid
| Date: Fri, 23 Sep 2005 13:42:01 -0700
| Lines: 145
| Message-ID: <27**********************************@microsoft.co m>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:124501
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| It worked. Thank you.
|
| "Steven Cheng[MSFT]" wrote:
|
| > Hi Cadel,
| >
| > Welcome to MSDN newsgroup.
| > From your descrption, you bind some option datas in a ComboBox (from a
| > datasource...), and add a Button, when user click that button, you want
to
| > add a new record to another DataTable which is bind to a DataGrid, and
the
| > data is coming from the datasource of the ComboBox control, yes?
| >
| > IMO, I think you can first put the DataSource initializing code for
both
| > the ComboBox and DataGrid into the Form's initializaing time, e.g , in
the
| > Form_Load event. e.g:
| >
| > private void Form1_Load(object sender, System.EventArgs e)
| > {
| > DataTable dtOptions = new DataTable("options");
| > dtOptions.Columns.Add("name",typeof(string));
| > dtOptions.Columns.Add("desc", typeof(string));
| >
| > for(int i=0; i<10; i++)
| > {
| > DataRow dr = dtOptions.NewRow();
| > dr[0] ="Name_" + i;
| > dr[1] = "Desc_" + i;
| >
| > dtOptions.Rows.Add(dr);
| > }
| >
| > cbOptions.DataSource =dtOptions;
| > cbOptions.DisplayMember = "name";
| > cbOptions.ValueMember = "desc";
| >
| >
| > DataTable dtItems = dtOptions.Clone();
| > dgItems.DataSource = dtItems;
| >
| > }
| >
| > And when we need to add new record into the DataGrid, we are actually
| > adding new record into the DataGrid's Binding DataSource(the underlying
| > datatable). So in your Add button's click event, our code is something
like:
| >
| >
| > private void btnAdd_Click(object sender, System.EventArgs e)
| > {
| >
| > DataRowView drv = cbOptions.SelectedItem as DataRowView;
| >
| > string name = (string)drv[0];
| > string desc = (string)drv[1];
| >
| > DataTable dt = dgItems.DataSource as DataTable;
| >
| > DataRow dr = dt.NewRow();
| >
| > dr[0] = name;
| > dr[1] = desc;
| >
| > dt.Rows.Add(dr);
| > }
| >
| >
| > Note that when adding new item into DataTable, we need to call
| > DataTable.NewRow to create a new DataRow instance, fill in the datas
and
| > add it into the Rows Colleciton.
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| >
| >
| > --------------------
| > | Thread-Topic: Combo Box to populate Data Grid
| > | thread-index: AcW/rmno1XNeCtNjQQOB1PkA3rAteg==
| > | X-WBNR-Posting-Host: 204.87.106.66
| > | From: =?Utf-8?B?TWlrZSBM?= <Ca***@nospam.nospam>
| > | Subject: Combo Box to populate Data Grid
| > | Date: Thu, 22 Sep 2005 12:47:07 -0700
| > | Lines: 30
| > | Message-ID: <C3**********************************@microsoft.co m>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="Utf-8"
| > | Content-Transfer-Encoding: 7bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | Content-Class: urn:content-classes:message
| > | Importance: normal
| > | Priority: normal
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.languages.csharp:124211
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > |
| > | I'm open for any suggestions on how to better program this. I want
the
| > user
| > | to select a license from a combo box, cboPrivilege and then the user
will
| > | click the add button, then a record will be added to the data grid.
| > |
| > | My current code is coming up with errors, dt and drv are unknown in
the
| > else
| > | part of the if statement.
| > |
| > |
| > | if (this.dgPrivileges.DataSource == null)
| > | {
| > | DataTable dt = new DataTable();
| > | dt.Columns.Add("Code",
typeof(string));
| > | dt.Columns.Add("Amount",
typeof(float));
| > | this.dgPrivileges.DataSource = dt;
| > |
| > | DataRowView
| > | drv=this.cboPrivilege.SelectedItem as DataRowView;
| > | dt.Rows.Add(new
| > | object[]{drv["LICENSE_CODE"].ToString(),
| > drv["GROSS_FEE_AMOUNT"].ToString()});
| > |
| > |
| > | cboPrivilege.Focus();
| > | }
| > | else
| > | {
| > | dt.Rows.Add(new
| > | object[]{drv["LICENSE_CODE"].ToString(),
| > drv["GROSS_FEE_AMOUNT"].ToString()});
| > | }
| > |
| > |
| >
| >
|

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.