Datagrid problem - access field of current row

dean.c4

Member
Joined
Nov 22, 2006
Messages
9
Programming Experience
5-10
Ok, I'm new to .NET, but not new to VB. But its been since VB5...so I'm rusty and crusty. What I'm trying to do used to take me minutes, now I'm just stumped over the simplest crap.

Background:
I have a form with a couple of text boxes, a datepicker control, a combobox and a datagrid. I have a SQL db with 3 tables in it. A Master, a detail and a 'site' table.

The datagrid is bound to the detail table via a DataSet. I just clicked in the empty 'Data Sources' tab/explorer and went to town...now I'm not convinced that I went the right route. I used to just code all the db stuff by hand, but I'd like to learn the new stuff.

So at the bottom of the designer, I have a DataSet, 3 table adapters, 3 binding sources and only one binding navigator for the detail table.

Anyway, datagrid is connected to the detail table. All of the other controls on the form are connected to the master table via the same method except for the combo box which only contains the 3 records from the Site table.

Ok, so the form opens, you fill in the boxes, then go down to the data grid to fill in the 3 fields that are visible, then you're done (commit changes to master and detail recordsets) or you add another detail record (tied to the same master record). The problem is that there is a field in the datagrid that I don't have visible which is the MasterID that I use to tie these detail records to the correct record in the Master table.

The Actual Question:
So, how do I set/edit the MasterID field value of the current row (detail table) in the datagrid = the MasterID field value of the currently selected record of the Master table?

I realize I'm probably using the term 'table' incorrectly since I'm dealing with a dataset...old habbits...
 
You may need this function to get the master table current selected row

Private Function getSelectedRow()
'Get the Currency Manager using the BindingContext of the DataGrid
Dim cm As CurrencyManager = Me.dgList.BindingContext(Me.dgList.DataSource)
If (cm.Count = 0) Then 'If the datagrid doesn't have rows
Return DBNull.Value
End If
'Get default DataView of the DataGrid
Dim drv As DataRowView = cm.Current
Return drv.Row 'myDataRow is the row selected
End Function

and the code to retrieve the child table rows:

Dim rows() as datarow = myDataSet.tables("TBL").select("MasterID = '123')
if rows.length > 0 then
' Loop through the rows and do whatever you want
end if
 
So, how do I set/edit the MasterID field value of the current row (detail table) in the datagrid = the MasterID field value of the currently selected record of the Master table?


Curiously enough, you dont :)

Your datatables hopefully have a datarelation between them that links from parent.id=child.id

When you add a new row to the master list, and then a new row to the child, the new child row gets the id of whichever master is the current row, regardles of its own default value settings. If there is no current master row.. well, you jkust broke a relatisnhip and can expect an exception to pop along soon :)

In code this would be:

MasterBindingSource.AddNew()
MasterBindingSource.EndEdit()
ChildBindingSource.AddNew()
ChildBindingSource.EndEdit()


If you dont put the endedits in there then you will run intoa datarelation bug whereby calling endedit on either BindingSource(BS) destroys the data in the other BS. Note that you should set the id of the master row to something before you add the child row. If this isnt done by auotnumbering then it should be done by setting the .DefaultValue of the datatable column in question before you call addnew on the master
 
Last edited:
cjard,
OK, that is what I'm trying to figure out how to do. I have the relationships setup in SQL, but no code yet to put the masterID into the child record, this is where I'm running into the problem. The id field in the master table is an identity (autonumbering) field, so that part is taken care of. But after I add a new master record, I want to copy whatever is in that records ID field (master table) and copy it into the master ID field of the child table, to tie them together.

I'm actually using a DataGridView, sorry. I'm just lost right now in what the code needs to look like to pull off this functionality. Can someone give me a generic example?
 
Back
Top