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?= <
[email protected]>
| References: <
[email protected]>
<
[email protected]>
| Subject: RE: Combo Box to populate Data Grid
| Date: Fri, 23 Sep 2005 13:42:01 -0700
| Lines: 145
| Message-ID: <
[email protected]>
| 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?= <
[email protected]>
| > | Subject: Combo Box to populate Data Grid
| > | Date: Thu, 22 Sep 2005 12:47:07 -0700
| > | Lines: 30
| > | Message-ID: <
[email protected]>
| > | 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()});
| > | }
| > |
| > |
| >
| >
|