1
votes

I have a Datagridview grid with four columns. When a cell is double-clicked, the data on the selected row is passed to four textboxes for the user to make changes if any.So how do i pass the changes made to the selected row instead of having the changes added as new row?

P.S. The Data isn't from a database

2
Is the data in the grid stored in a DataTable? Is it a view of a table in the database? Show some code that sets the DataSource of the grid.David
No the data isn't from a database.Nick Mackenzie
What's the primary key called? I'm assuming that there is some sort of a unique identifier in the data?David
There is a RefNo Column.Thats my unique identifierNick Mackenzie

2 Answers

1
votes

You can either "remember" the DataGridViewRow by setting a module-level variable, or you can find the row again by looking for its primary key.

Public Class Form1
  'Add to form:
  ' DataGridView called DataGridView1
  ' 4 Textboxes called TextBox1, TextBox2, TextBox3, and TextBox4
  ' Button called btnEdit
  ' Button called btnSave

  Private mintRowWeAreEditing As Integer = -1
  Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
    If DataGridView1.DataSource Is Nothing Then
      'set initial data
      Dim dtb As New DataTable
      dtb.Columns.Add("Col1")
      dtb.Columns.Add("Col2")
      dtb.Columns.Add("Col3")
      dtb.Columns.Add("Col4")
      dtb.Rows.Add("R1C1", "R1C2", "R1C3", "R1C4")
      dtb.Rows.Add("R2C1", "R2C2", "R2C3", "R2C4")
      dtb.Rows.Add("R3C1", "R3C2", "R3C3", "R3C4")
      dtb.Rows.Add("R4C1", "R4C2", "R4C3", "R4C4")
      DataGridView1.DataSource = dtb
    End If
    'copy data from grid to textboxes
    mintRowWeAreEditing = DataGridView1.CurrentCell.RowIndex
    Dim drw As DataRow = DirectCast(DataGridView1.Rows(mintRowWeAreEditing).DataBoundItem, DataRowView).Row
    TextBox1.Text = drw("Col1").ToString
    TextBox2.Text = drw("Col2").ToString
    TextBox3.Text = drw("Col3").ToString
    TextBox4.Text = drw("Col4").ToString
  End Sub

  Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    'copy data from textboxes to grid
    If mintRowWeAreEditing = -1 Then Exit Sub 'haven't clicked Edit button yet
    Dim drw As DataRow = DirectCast(DataGridView1.Rows(mintRowWeAreEditing).DataBoundItem, DataRowView).Row
    drw("Col1") = TextBox1.Text
    drw("Col2") = TextBox2.Text
    drw("Col3") = TextBox3.Text
    drw("Col4") = TextBox4.Text
  End Sub
End Class
1
votes

So, for example, you could do something like this on your button click event.

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click


  Dim refNo As Integer = txtRefNo.Text     ' Change DataType and TextBox name as appropriate
  Dim firstName as String = txtFName.Text
    ' Repeat setting variables for each field in the row that you're updating

  For Each dgr As DataGridViewRow in DataGridView1.Rows
    If dgr.Item("RefNo") = refNo Then
      dgr.Cells(0).Value = firstName 'Instead of using (0) you can use the column name
      dgr.Cells(1).Value = newVar
    End If
  Next

 ' Commit the changes/refresh here

End Sub

I don't have the IDE to hand to test this but any problems let me know, I'll have a look into it.