DataGrid to TextBox?

  • Thread starter Thread starter MsSunshine
  • Start date Start date
M

MsSunshine

Guest
I program in VB.Net and use SQL server. What I want to do is select a Row in the DataGrid and have that record fall into textboxes in another form so it can be edited. Any suggestions?
 
well, if your datagrid is populated by a dataset, put some code in the Select (or whatever the event name is) event something like this:

txtField1.Text=myDataTable.Rows(myDataGrid.CurrentRowIndex)(Field_Name)

or

txtField1.Text=myDataSet.Tables(Table_Name).Rows(myDataGrid.CurrentRowIndex)(Field_Name)

does that help? This is, of course, assuming you ar not hiding any rows in your datagrid.

Now, if you're putting it into another form, there's a whole bunch of ways to do that, but probably having a public myDataRow variable in a module would be the simplest:

Public myDataRow as DataRow

then in your code, on the CurrentCellChanged event, you could :

myDataRow=myDataTable.Rows(myDataGrid.CurrentRowIndex)
dim frm2 as New frmForm2


or something to that effect... that obviously not all you need, you'll have to do some work in the Form_Load event and stuff, and depending on what you need to do, it can get really complicated really fast. But that's the gist of it.
 
Hi Vax:
Now this is what I was looking forward.......thank you SOOOO much. I knew what I had to do but just could not think of the right Code. Thank you.
 
No problem... it's nice to help someone else out instead of the other way around!

-Vax
 
oh, one more little thing......(if I work on it long enough I know I could figure it out but thought you might have a quick idea).....my DataGrid is on one Form and then when they select a row it closes that form and open the new Form with the textboxes....and...of course that Form does not recognize the DataGrid. How do I reference it to the next Form?
 
with the global DataRow, set in a module:

Module modGlobals

Public myDataRow as DataRow

End Module


then set the global variable in the original form, then open a new form, then close the old form. In the new form, reference the global variable in the Form_Load event for data extraction, set it to a local variable, and go to town.
 
Back
Top