Datagrid OnClick event to display row's data to another form2

bizjosh

Member
Joined
Mar 16, 2005
Messages
23
Programming Experience
Beginner
Hi,

I'm using a data adapter 'da1' to fill a dataset 'ds1' which is used
to display data in a datagrid 'dg'. How do I code datagrid OnClick event
on whichever row the user click on the datagrid, to cause a new
form2 to popup with textboxes to display the selected row's data
and allow the user to edit the data and click 'update' so it will
be updated to the database. The user can alt-tab to switch to main
form and select another row, the new's row data will be displayed on
form2's textboxes and editable too.

Any idea how i can do that?

da1
---
Table1: ID, Name, Age, Job
Table2: ID, Status, Salary

I did not include the other tables into 'da1' , however this other
tables data will be needed to display on form2's textboxes, is it
neccessary for me to include it into 'da1' or should i create another
data adapter 'da2' for form2's displaying/updating?

Please advise, help appreciated!
Thanks.
 
Use something like this:
VB.NET:
[color=Blue]Private Sub[/color] DataGrid1_Click(...) [color=Blue]Handles[/color] DataGrid1.Click
  [color=Green]'Instantiate the update dialogue.[/color]
  [color=Blue]Dim[/color] updateDialogue [color=Blue]As New[/color] UpdateForm

  [color=Green]'Set the values in the dialogue TextBoxes from the selected row in the Datagrid.[/color]
  updateDialogue.TextBox1.Text = [color=Blue]Me[/color].DataGrid1.Item([color=Blue]Me[/color].DataGrid1.CurrentRowIndex, 0).ToString()
  updateDialogue.TextBox2.Text = [color=Blue]Me[/color].DataGrid1.Item([color=Blue]Me[/color].DataGrid1.CurrentRowIndex, 1).ToString()

  [color=Green]'Display the update dialogue.[/color]
  [color=Blue]If[/color] updateDialogue.ShowDialog() <> DialogResult.OK [color=Blue]Then[/color]
	[color=Blue]Exit Sub[/color]
  [color=Blue]End If[/color]

  [color=Green]'Update the DataGrid from the dialogue.[/color]
  [color=Blue]Me[/color].DataGrid1.Item([color=Blue]Me[/color].DataGrid1.CurrentRowIndex, 0) = updateDialogue.TextBox1.Text
  [color=Blue]Me[/color].DataGrid1.Item([color=Blue]Me[/color].DataGrid1.CurrentRowIndex, 1) = updateDialogue.TextBox2.Text
[color=Blue]End Sub[/color]
Personally, I would be inclined to use the DoubleClick event to edit a row rather than the Click event. You could pass the values to the constructor of the update dialogue. You could also set and/or retrieve the values through public properties of the dialogue rather than the TextBoxes directly.
 
Back
Top