PC Review


Reply
Thread Tools Rate Thread

Combo Box to populate Data Grid

 
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      22nd Sep 2005

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()});
}

 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      23rd Sep 2005
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?= <(E-Mail Removed)>
| Subject: Combo Box to populate Data Grid
| Date: Thu, 22 Sep 2005 12:47:07 -0700
| Lines: 30
| Message-ID: <C3A39129-9F09-45F1-BC2A-(E-Mail Removed)>
| 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()});
| }
|
|

 
Reply With Quote
 
Kav
Guest
Posts: n/a
 
      23rd Sep 2005
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" <(E-Mail Removed)> wrote in message
news:C3A39129-9F09-45F1-BC2A-(E-Mail Removed)...
>
> 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()});
> }
>



 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      23rd Sep 2005
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?= <(E-Mail Removed)>
> | Subject: Combo Box to populate Data Grid
> | Date: Thu, 22 Sep 2005 12:47:07 -0700
> | Lines: 30
> | Message-ID: <C3A39129-9F09-45F1-BC2A-(E-Mail Removed)>
> | 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()});
> | }
> |
> |
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      26th Sep 2005
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?= <(E-Mail Removed)>
| References: <C3A39129-9F09-45F1-BC2A-(E-Mail Removed)>
<(E-Mail Removed)>
| Subject: RE: Combo Box to populate Data Grid
| Date: Fri, 23 Sep 2005 13:42:01 -0700
| Lines: 145
| Message-ID: <275ED35F-C831-4CEF-8CEF-(E-Mail Removed)>
| 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?= <(E-Mail Removed)>
| > | Subject: Combo Box to populate Data Grid
| > | Date: Thu, 22 Sep 2005 12:47:07 -0700
| > | Lines: 30
| > | Message-ID: <C3A39129-9F09-45F1-BC2A-(E-Mail Removed)>
| > | 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()});
| > | }
| > |
| > |
| >
| >
|

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
populate a wpf combo with data from XAML wpfRookie Microsoft C# .NET 0 10th Feb 2009 08:58 AM
How to Assign a Combo box in Data grid =?Utf-8?B?Tmlsb3RwYWw=?= Microsoft Access 0 3rd Jul 2007 01:44 PM
Combo Box in .Net Data Grid surf2mail@gmail.com Microsoft VB .NET 0 1st May 2007 08:26 PM
How to re-populate a Data Grid? =?Utf-8?B?U25ha2U=?= Microsoft Dot NET 3 19th Oct 2005 12:04 PM
What code has the best performance to populate a Data Grid with a =?Utf-8?B?TWlrZSBM?= Microsoft C# .NET 8 28th Jul 2005 08:18 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:48 PM.