How to populate textboxes with the data from a datagrid when a row is selected?
I currently have a datagrid that I have populated from a dataset and I need to be able to populate some textboxes on my form with the data is the datagrid on the row I select?
I have only been learning VB.net in my new role for 2 weeks now and would appreciate any help.
Thanks
Re: How to populate textboxes with the data from a datagrid when a row is selected?
you can bind the textboxes to seperate fields in the same datatable you've bound your dgv with. as you select a new row in the dgv, the textboxes will update with the information from that row.
here's how you'd do it:
Textbox1.DataBindings.Add("Text", [datatable], "fieldName")
Re: How to populate textboxes with the data from a datagrid when a row is selected?
Morning, thanks for your reply.
I've tried what you have put first thing when I got to work this morning and it is coming up with an error. It is sayin:
'ArguamentException was unhandled' Cannot bind to property or column part_description on the datasource. Parameter name: datamember.
After a bit of exploring the help files I have managed to display the text via the following method:
txtPartDescription.Text = dgvPartsData.CurrentRow.Cells(1).FormattedValue.ToString
I'm not sure if it is the best practise way of doing it though?
Re: How to populate textboxes with the data from a datagrid when a row is selected?
databinding is the best method. can you post the exact code you tried?
Re: How to populate textboxes with the data from a datagrid when a row is selected?
I just added the following:
Mainform.txtPartNumber.DataBindings.Add("Text", [partsDataSet], "part_description")
Re: How to populate textboxes with the data from a datagrid when a row is selected?
partsDataSet appears to be a dataset. you need to bind to a datatable
Re: How to populate textboxes with the data from a datagrid when a row is selected?
Yes your correct it is a dataset. I will read up on how to bind it and give it a go.