I have a problem with linking a datagridview to text boxes on a form.
I want to be able to click a row on the dgv and populate the fields of the associated form. Everything works fine, until I filter the dgv or sort it, at which point the relationship between the data on the dgv and the form is lost. I realise that this is because the first row in the dgv only relates to the first record in my dataset up until the point where I sort or filter the dgv, but what I can't get my head round is how to overcome this and achieve what I want to.
For the unsorted dgv, the code is simple...
Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
intPtr = DataGridView1.CurrentRow.Index
Public Sub usersubFillEntrantForm(ByVal index)
'sub to fill entrantControl with records from dataset
EntrantControl1.urnLabel.Text = .Item("urn").ToString
EntrantControl1.surnameTextBox.Text = .Item("surname").ToString
EntrantControl1.firstnameTextBox.Text = .Item("firstname").ToString
My dataset includes a unique reference number (the "urn" column within the dataset) and I know that somehow I must be able to use this to achieve the result I want. After all, when I designed the dataset I thought the "urn" would come in handy, which is why I put it there. Perhaps it's just because it's late in the evening, but I can't figure it out.
In case you haven't guessed, I'm an amateur...
Any tips to resolve this would be greatly appreciated!
You can bind as many TextBoxes and other controls as you need for the fields in a record. When you you select a row in the grid, the fields of that row will automatically be displayed in the individual controls. Any edits you make in the individual controls will automatically be reflected in the grid. If you ever want to force pending changes to be committed, e.g. immediately before saving back to the database, you call EndEdit on the BindingSource. because all controls are bound to the BindingSource, they will all respect any changes you make to the Sort and Filter properties of that BindingSource.
I've spent ages coding different routines to get the data from the datagridview onto the form and was quite pleased with myself...only to find there was a built in way to do it!
Like I say, I'm a complete amateur and as I develop this little application, I am learning new things all the time. Using databindings as you have suggested is making my coding so much easier, though I've got to back track quite a long way now...
All good fun though. Thanks so much for your help.