473,573 Members | 4,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combo Box to populate Data Grid


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.dgPrivile ges.DataSource == null)
{
DataTable dt = new DataTable();
dt.Columns.Add( "Code", typeof(string)) ;
dt.Columns.Add( "Amount", typeof(float));
this.dgPrivileg es.DataSource = dt;

DataRowView
drv=this.cboPri vilege.Selected Item as DataRowView;
dt.Rows.Add(new
object[]{drv["LICENSE_CO DE"].ToString(), drv["GROSS_FEE_AMOU NT"].ToString()});
cboPrivilege.Fo cus();
}
else
{
dt.Rows.Add(new
object[]{drv["LICENSE_CO DE"].ToString(), drv["GROSS_FEE_AMOU NT"].ToString()});
}

Nov 17 '05 #1
4 2994
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(obje ct sender, System.EventArg s e)
{
DataTable dtOptions = new DataTable("opti ons");
dtOptions.Colum ns.Add("name",t ypeof(string));
dtOptions.Colum ns.Add("desc", typeof(string)) ;

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

dtOptions.Rows. Add(dr);
}

cbOptions.DataS ource =dtOptions;
cbOptions.Displ ayMember = "name";
cbOptions.Value Member = "desc";
DataTable dtItems = dtOptions.Clone ();
dgItems.DataSou rce = 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(ob ject sender, System.EventArg s e)
{

DataRowView drv = cbOptions.Selec tedItem as DataRowView;

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

DataTable dt = dgItems.DataSou rce 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.NewRo w 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/rmno1XNeCtNjQQO B1PkA3rAteg==
| X-WBNR-Posting-Host: 204.87.106.66
| From: =?Utf-8?B?TWlrZSBM?= <Ca***@nospam.n ospam>
| Subject: Combo Box to populate Data Grid
| Date: Thu, 22 Sep 2005 12:47:07 -0700
| Lines: 30
| Message-ID: <C3************ *************** *******@microso ft.com>
| 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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GXA03.phx.gbl
| Xref: TK2MSFTNGXA01.p hx.gbl
microsoft.publi c.dotnet.langua ges.csharp:1242 11
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.dgPrivile ges.DataSource == null)
| {
| DataTable dt = new DataTable();
| dt.Columns.Add( "Code", typeof(string)) ;
| dt.Columns.Add( "Amount", typeof(float));
| this.dgPrivileg es.DataSource = dt;
|
| DataRowView
| drv=this.cboPri vilege.Selected Item as DataRowView;
| dt.Rows.Add(new
| object[]{drv["LICENSE_CO DE"].ToString(),
drv["GROSS_FEE_AMOU NT"].ToString()});
|
|
| cboPrivilege.Fo cus();
| }
| else
| {
| dt.Rows.Add(new
| object[]{drv["LICENSE_CO DE"].ToString(),
drv["GROSS_FEE_AMOU NT"].ToString()});
| }
|
|

Nov 17 '05 #2
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.n ospam> wrote in message
news:C3******** *************** ***********@mic rosoft.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.dgPrivile ges.DataSource == null)
{
DataTable dt = new DataTable();
dt.Columns.Add( "Code", typeof(string)) ;
dt.Columns.Add( "Amount", typeof(float));
this.dgPrivileg es.DataSource = dt;

DataRowView
drv=this.cboPri vilege.Selected Item as DataRowView;
dt.Rows.Add(new
object[]{drv["LICENSE_CO DE"].ToString(),
drv["GROSS_FEE_AMOU NT"].ToString()});
cboPrivilege.Fo cus();
}
else
{
dt.Rows.Add(new
object[]{drv["LICENSE_CO DE"].ToString(),
drv["GROSS_FEE_AMOU NT"].ToString()});
}

Nov 17 '05 #3
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(obje ct sender, System.EventArg s e)
{
DataTable dtOptions = new DataTable("opti ons");
dtOptions.Colum ns.Add("name",t ypeof(string));
dtOptions.Colum ns.Add("desc", typeof(string)) ;

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

dtOptions.Rows. Add(dr);
}

cbOptions.DataS ource =dtOptions;
cbOptions.Displ ayMember = "name";
cbOptions.Value Member = "desc";
DataTable dtItems = dtOptions.Clone ();
dgItems.DataSou rce = 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(ob ject sender, System.EventArg s e)
{

DataRowView drv = cbOptions.Selec tedItem as DataRowView;

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

DataTable dt = dgItems.DataSou rce 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.NewRo w 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/rmno1XNeCtNjQQO B1PkA3rAteg==
| X-WBNR-Posting-Host: 204.87.106.66
| From: =?Utf-8?B?TWlrZSBM?= <Ca***@nospam.n ospam>
| Subject: Combo Box to populate Data Grid
| Date: Thu, 22 Sep 2005 12:47:07 -0700
| Lines: 30
| Message-ID: <C3************ *************** *******@microso ft.com>
| 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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GXA03.phx.gbl
| Xref: TK2MSFTNGXA01.p hx.gbl
microsoft.publi c.dotnet.langua ges.csharp:1242 11
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.dgPrivile ges.DataSource == null)
| {
| DataTable dt = new DataTable();
| dt.Columns.Add( "Code", typeof(string)) ;
| dt.Columns.Add( "Amount", typeof(float));
| this.dgPrivileg es.DataSource = dt;
|
| DataRowView
| drv=this.cboPri vilege.Selected Item as DataRowView;
| dt.Rows.Add(new
| object[]{drv["LICENSE_CO DE"].ToString(),
drv["GROSS_FEE_AMOU NT"].ToString()});
|
|
| cboPrivilege.Fo cus();
| }
| else
| {
| dt.Rows.Add(new
| object[]{drv["LICENSE_CO DE"].ToString(),
drv["GROSS_FEE_AMOU NT"].ToString()});
| }
|
|

Nov 17 '05 #4
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+ioay 1S6rft3g==
| X-WBNR-Posting-Host: 204.87.106.66
| From: =?Utf-8?B?TWlrZSBM?= <Ca***@nospam.n ospam>
| References: <C3************ *************** *******@microso ft.com>
<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************ *************** *******@microso ft.com>
| 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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GXA03.phx.gbl
| Xref: TK2MSFTNGXA01.p hx.gbl
microsoft.publi c.dotnet.langua ges.csharp:1245 01
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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(obje ct sender, System.EventArg s e)
| > {
| > DataTable dtOptions = new DataTable("opti ons");
| > dtOptions.Colum ns.Add("name",t ypeof(string));
| > dtOptions.Colum ns.Add("desc", typeof(string)) ;
| >
| > for(int i=0; i<10; i++)
| > {
| > DataRow dr = dtOptions.NewRo w();
| > dr[0] ="Name_" + i;
| > dr[1] = "Desc_" + i;
| >
| > dtOptions.Rows. Add(dr);
| > }
| >
| > cbOptions.DataS ource =dtOptions;
| > cbOptions.Displ ayMember = "name";
| > cbOptions.Value Member = "desc";
| >
| >
| > DataTable dtItems = dtOptions.Clone ();
| > dgItems.DataSou rce = 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(ob ject sender, System.EventArg s e)
| > {
| >
| > DataRowView drv = cbOptions.Selec tedItem as DataRowView;
| >
| > string name = (string)drv[0];
| > string desc = (string)drv[1];
| >
| > DataTable dt = dgItems.DataSou rce 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.NewRo w 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/rmno1XNeCtNjQQO B1PkA3rAteg==
| > | X-WBNR-Posting-Host: 204.87.106.66
| > | From: =?Utf-8?B?TWlrZSBM?= <Ca***@nospam.n ospam>
| > | Subject: Combo Box to populate Data Grid
| > | Date: Thu, 22 Sep 2005 12:47:07 -0700
| > | Lines: 30
| > | Message-ID: <C3************ *************** *******@microso ft.com>
| > | 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.publi c.dotnet.langua ges.csharp
| > | NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.2.250
| > | Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GXA03.phx.gbl
| > | Xref: TK2MSFTNGXA01.p hx.gbl
| > microsoft.publi c.dotnet.langua ges.csharp:1242 11
| > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.dgPrivile ges.DataSource == null)
| > | {
| > | DataTable dt = new DataTable();
| > | dt.Columns.Add( "Code",
typeof(string)) ;
| > | dt.Columns.Add( "Amount",
typeof(float));
| > | this.dgPrivileg es.DataSource = dt;
| > |
| > | DataRowView
| > | drv=this.cboPri vilege.Selected Item as DataRowView;
| > | dt.Rows.Add(new
| > | object[]{drv["LICENSE_CO DE"].ToString(),
| > drv["GROSS_FEE_AMOU NT"].ToString()});
| > |
| > |
| > | cboPrivilege.Fo cus();
| > | }
| > | else
| > | {
| > | dt.Rows.Add(new
| > | object[]{drv["LICENSE_CO DE"].ToString(),
| > drv["GROSS_FEE_AMOU NT"].ToString()});
| > | }
| > |
| > |
| >
| >
|

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2019
by: kk | last post by:
Have 2 problems, any help is appreciated. Tab with Grids -------------- BL - fetching data from DB ( 5 secs - 10 rows) Grid Laod - 20 secs Grid Paint on tab change - 20 secs Problem: The data fetch only takes 5 secs, why does paint and load take 40 secs in total.
1
2049
by: kk | last post by:
I have 2 issues, please help ! 1st issue : -------------------------------------------------------- Loading Grid ------------ 1) creating new rows in a datatable and adding data a array list to datatable.
9
3313
by: Mike L | last post by:
I tried a sample of code in MSDN magazine, but now I'm stuck. What code has the best performance to populate a Data Grid with a SP? Below is the code I have, which might be completing the wrong way to populate a data grid. I like using code and not the server explorer. (I replaced the sa password for this post.) private void...
4
301
by: Ziv Riezman | last post by:
Hi All I Have a datagrid on asp.net. i need to add a combo box column that chages it's data according to a spefiiec row in a datagrid. Please help me ?? Thnaks Ziv Riezman
2
2016
by: Brian Henry | last post by:
Hi, I have a data grid that is set up like this Page items displayed = 10 EnableViewState = false (i dont want to send large amounts of data over the internet!) CustomPaging = false AllowPaging = true
6
3746
by: Ron L | last post by:
I have a dataset whose source is a SQL 2k stored procedure that I am trying to display in a datagrid. This datasource has 4 columns that I am interested in here, a text column and 3 value columns corresponding to permissions to certain data classes. I want to put the permission values in combo boxes in the grid and instead of displaying the...
9
9637
by: bill.jenner | last post by:
I have two related tables I wish to use in a grid. The second table has to drive the list of items apart of a combo box apart of one of the grids columns. Table 1 schema has columns ( . . . . "Department" . . . ) and needs to populate the entire grid. Table 2 schema had one column ("Department") and needs to populate a combo box attached to...
4
13948
by: whamo | last post by:
I have the need to populate a field based on the selection in a combo box. Starting out simple. (2) tables tbl_OSE_Info and tbl_Input; tbl_OSE_Info has three fields: Key, OSE_Name and OSE_Wt tbl_Input has three fields: OSE_Job, OSE_Name, OSE_Wt I have populated tbl_OSE_Info table. I need to create a form that will store the data in...
1
2238
by: pavanip | last post by:
Hi, I have a data grid control and I placed one combo box in that data grid and I am binding data to data grid combo box.When i run the application by default the combo box is not showing anything if i double click on combo box then only it is displaying the items. Can anybody please help me to resolve the problem showing combo box items in...
0
7707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7800
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8086
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6432
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2227
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1325
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1048
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.