none
Updating row from datagridview RRS feed

  • Question

  • Hello,

    Can someone could give me an example how to update selected row in datagridview via textbox. I'm using access database. Thank you.


    • Edited by SimmersM Monday, May 7, 2018 4:44 PM
    Monday, May 7, 2018 4:43 PM

All replies

  • If the DataGridView has it's DataSource set to say a DataTable then consider creating a BindingSource, set the BindingSource.DataSource to a DataTable. The BindingSource is then set to the DataSource of the DataGridView.

    To get the current DataRow represented by the current DataGridView row.

    Dim row As DataRow = CType(SomeBindingSource.Current,DataRowView).Row

    You now have access to the row. To get access to a field in the row.

    row.Field(Of String)("FirstName")

    Note the reason I recommend the above is you indicated "update" which leads me to think you are obtaining data from a ms-access table. If so you would need to primary key which when reading data into a DataTable you can a) assign columns in the DataGridView but not for the primary key yet can still get the key via the BindingSource idea.

    Again being the case you read from the database (and there are many ways) you simply write a UPDATE statement and execute ExecuteNonQuery on the command object or if using TableAdapters press the save button or whatever makes sense.

    I have a demo over on Microsoft's code site. The demo shows all I have written above and more.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, May 7, 2018 5:28 PM
    Moderator
  • Hi Karen,

    Thank you for your help and link.

    Monday, May 7, 2018 5:36 PM
  • Hi SimmersM,

    You want to like this?

    Public Class FrmupdateDatagridview
        Dim table As New DataTable("Table")
        Dim index As Integer
        Private Sub FrmupdateDatagridview_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            table.Columns.Add("Id", Type.GetType("System.Int32"))
            table.Columns.Add("First Name", Type.GetType("System.String"))
            table.Columns.Add("Last Name", Type.GetType("System.String"))
            table.Columns.Add("Age", Type.GetType("System.Int32"))
    
            ' Add rows to the datatable with some data
    
            table.Rows.Add(1, "XXXX", "YYYYY", 21)
            table.Rows.Add(2, "SSDD", "hGSQ", 33)
            table.Rows.Add(3, "fgfgd", "jgfdd", 53)
            table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
            table.Rows.Add(5, "hghfd", "ghjgdf", 36)
            table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)
            DataGridView1.DataSource = table
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' the new row
            Dim newDataRow As DataGridViewRow
            ' get data from textboxes to the row
            newDataRow = DataGridView1.Rows(index)
            newDataRow.Cells(0).Value = TextBox1.Text
            newDataRow.Cells(1).Value = TextBox2.Text
            newDataRow.Cells(2).Value = TextBox3.Text
            newDataRow.Cells(3).Value = TextBox4.Text
    
        End Sub
    
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            ' get the index of the selected datagridview row
            index = e.RowIndex
            Dim selectedRow As DataGridViewRow
            ' display data from datagridview selected row to textboxes
    
            selectedRow = DataGridView1.Rows(index)
    
            TextBox1.Text = selectedRow.Cells(0).Value.ToString()
            TextBox2.Text = selectedRow.Cells(1).Value.ToString()
            TextBox3.Text = selectedRow.Cells(2).Value.ToString()
            TextBox4.Text = selectedRow.Cells(3).Value.ToString()
        End Sub
    End Class

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Thursday, May 10, 2018 9:31 AM
    Tuesday, May 8, 2018 1:39 AM
    Moderator