Adding a new row to DataSource [modified]
-
Hello all, I have been pulling my hair our trying to get this to work. I created a DataSource programatically and applied it to a DataGridView. Now, I want to add rows to that DataSource programatically and have it displayed in the DataGridView, but I can't for the life of me work out how to add to the DataSource.... I know there must be a relatively simple way to do this.. I can't add data to it in the usual way because the DataGridView is bound. Any advice is much appreciated! =) Here's the code for creating the DataSource and adding columns to it on form.load, and showing it in my DataGridView (named "dg"). This part of the code works fine:
Dim Table1 As DataTable
Table1 = New DataTable("Useful Links")
'------------------------------------------------------------
Dim adModel As DataColumn = New DataColumn("adModel")
adModel.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(adModel)Dim unId As DataColumn = New DataColumn("unId") unId.DataType = System.Type.GetType("System.String") Table1.Columns.Add(unId) '------------------------------------------------------------ Dim ds As New DataSet() ds.Tables.Add(Table1) dg.DataSource = Table1 '------------------------------------------------------------
There will be a button (or something) to add rows to that DataSource.
modified on Wednesday, March 17, 2010 9:14 PM
-
Hello all, I have been pulling my hair our trying to get this to work. I created a DataSource programatically and applied it to a DataGridView. Now, I want to add rows to that DataSource programatically and have it displayed in the DataGridView, but I can't for the life of me work out how to add to the DataSource.... I know there must be a relatively simple way to do this.. I can't add data to it in the usual way because the DataGridView is bound. Any advice is much appreciated! =) Here's the code for creating the DataSource and adding columns to it on form.load, and showing it in my DataGridView (named "dg"). This part of the code works fine:
Dim Table1 As DataTable
Table1 = New DataTable("Useful Links")
'------------------------------------------------------------
Dim adModel As DataColumn = New DataColumn("adModel")
adModel.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(adModel)Dim unId As DataColumn = New DataColumn("unId") unId.DataType = System.Type.GetType("System.String") Table1.Columns.Add(unId) '------------------------------------------------------------ Dim ds As New DataSet() ds.Tables.Add(Table1) dg.DataSource = Table1 '------------------------------------------------------------
There will be a button (or something) to add rows to that DataSource.
modified on Wednesday, March 17, 2010 9:14 PM
-
Check it . http://msdn.microsoft.com/en-us/library/5ycd1034(VS.80).aspx[^]
If you can think then I Can.
Thanks for the response but this doesn't appear to be what I'm after... I am no expert (particularly with datatables) but the method you suggested appears to require a DataSet. Like mentioned earlier, my data is created at runtime (called Table1) on form.load so I am unsure how to refer to it programmatically... Forgive me if I am not making sense, or going the wrong way about all this, as previously mentioned I am not disciplined in datatables! :sigh:
-
Thanks for the response but this doesn't appear to be what I'm after... I am no expert (particularly with datatables) but the method you suggested appears to require a DataSet. Like mentioned earlier, my data is created at runtime (called Table1) on form.load so I am unsure how to refer to it programmatically... Forgive me if I am not making sense, or going the wrong way about all this, as previously mentioned I am not disciplined in datatables! :sigh:
-
Hello all, I have been pulling my hair our trying to get this to work. I created a DataSource programatically and applied it to a DataGridView. Now, I want to add rows to that DataSource programatically and have it displayed in the DataGridView, but I can't for the life of me work out how to add to the DataSource.... I know there must be a relatively simple way to do this.. I can't add data to it in the usual way because the DataGridView is bound. Any advice is much appreciated! =) Here's the code for creating the DataSource and adding columns to it on form.load, and showing it in my DataGridView (named "dg"). This part of the code works fine:
Dim Table1 As DataTable
Table1 = New DataTable("Useful Links")
'------------------------------------------------------------
Dim adModel As DataColumn = New DataColumn("adModel")
adModel.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(adModel)Dim unId As DataColumn = New DataColumn("unId") unId.DataType = System.Type.GetType("System.String") Table1.Columns.Add(unId) '------------------------------------------------------------ Dim ds As New DataSet() ds.Tables.Add(Table1) dg.DataSource = Table1 '------------------------------------------------------------
There will be a button (or something) to add rows to that DataSource.
modified on Wednesday, March 17, 2010 9:14 PM
If you're just binding the DGV to a single table, with no relations to other tables, you don't even need the DataSet. A DataSet is just a collection of DataTable object and DataRelations between them. Since your DGV is bound to a DataTable, you just need to add a new row to the DataTable, not to the DGV. Look into the DataTable.NewRow method. An alternative to doing it manually would be to create a BindingSource object, set it's DataSource to your DataTable, then set the DGV's DataSource to the BindingSource you created. All navigation and row handling is handled by the BindingSource object.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
If you're just binding the DGV to a single table, with no relations to other tables, you don't even need the DataSet. A DataSet is just a collection of DataTable object and DataRelations between them. Since your DGV is bound to a DataTable, you just need to add a new row to the DataTable, not to the DGV. Look into the DataTable.NewRow method. An alternative to doing it manually would be to create a BindingSource object, set it's DataSource to your DataTable, then set the DGV's DataSource to the BindingSource you created. All navigation and row handling is handled by the BindingSource object.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Thanks for the explanation Dave, that helped alot. Gonna try a few of your suggestions now and see how I go :)