How to add a row to a datagrid

lsdev

Well-known member
Joined
Jan 20, 2008
Messages
61
Programming Experience
1-3
I have two combo boxes and a command button, basically I want to click the button to add the values to my data grid (which is bound), so that I can add as many as I like then append it to the database. How do I achieve this?
 
Sorry, what what members of the the datagrid are bound?

If you mean it has a datasource as a table or dataset then you just need to add rows to the table.

Dim newRow as DataRow = myTable.NewRow()
row(0) = ComboBox1.SelectedValue
row(1) = ComboBox2.SelectedValue
myTable.Rows.Add(newRow)


If you're asking something different can you clarify please.
 
Sorry, what what members of the the datagrid are bound?

If you mean it has a datasource as a table or dataset then you just need to add rows to the table.

Dim newRow as DataRow = myTable.NewRow()
row(0) = ComboBox1.SelectedValue
row(1) = ComboBox2.SelectedValue
myTable.Rows.Add(newRow)


If you're asking something different can you clarify please.

No, thats exactly what I was after. I knew I had to add it to the table as a new datarow just didn't know the correct syntax as I was getting error's. Just to add the data grid has three rowa, but the third row (an ID number for the master) is hidden is there away for passingthat value in? Or is that done automatically for you?

Thanks
 
I have two combo boxes and a command button, basically I want to click the button to add the values to my data grid (which is bound), so that I can add as many as I like then append it to the database. How do I achieve this?

You dont add rows to a grid! Read up on MVC. You add rows to a data model (DataTable in your case) and the grid updates itself to show the changes.

VB.NET:
Sub ButtonHandler(ClickEventArgs whatever) Handles Button.Click
  
  Me.myDataSetInstance.MyDataTable.AddMyWhateverRow(combo1.SelectedValue, combo2.SelectedValue, "other field", "another field", 123, 456, true, false)

End Sub
 
No, thats exactly what I was after.

Yeah, except, it's not exactly what youre after, because it's generic.. DataRow, DataTable..

Use the type specific stuff you have generated, otherwise there is no point designing a typed-dataset. ROcksteady is on 1.1; things are different there, datasets dont link as well with databases, its a lot of manual work and 1.1 guys tend not to use the type specific ability.. It sounds harsh, but if youre on 2,0 be wary of advice from 1.1 guys because it's technically out of date by the very nature of the restrictions of the framework version
 
Just to add the data grid has three rowa, but the third row (an ID number for the master) is hidden is there away for passingthat value in?


Mmmh... take a read on MVC. Youre editing the model, which has 3 columns.. You must provide 3 columns, it doesnt matter if your grid shows 2, or 10, or 578 (575 calculated from the 3 ;) ).. Youre editing the model, the model has 3 columns, you must provide 3 columns.. You really need to get away from the idea that the grid has anything to do with the data, or you edits of the data having anything to do with the grid..

If you have set the columns to autogenerate an entry for you then you dont have to provide a value.. Ask the datatable for a new row:

Dim ro as MyTypedDataRow = myTypedDataSetInstance.MyTypedDataTableName.NewMyTypedDataRowRow()

NewRow() ' returns a generic row (type specific boxed as generic)
NewMyTypedDataRowRow() ' returns a type specific row

The ID value will be prepoulated for you(if the autoincrement options are set):
MessageBox.Show(ro.ID)

or you can set it yourself:
ro.ID = 123456






If you havent so far been exposed to the concept of "boxing" in an OO language, have a quick google for it..
 
I have now got it to work, thanks for your input. So I'm now updating the data table and then updating the database in one go. Thanks again:)
 
Back
Top