Darkchili Slayer Darkchili Slayer - 1 month ago 14x
Vb.net Question

VB.NET: Edit DataGridView Row by Textbox

I am new to stack overflow so sorry if I mess something up.

Basically what I have so far is DataGridView called DataDataGridView on Form1. When one of the rows are clicked it passes the data in the row from each column onto some textboxes on Form2, each textbox holding the data for a seperate column.

Form2.TextBox1.Text = DataDataGridView.Rows(e.RowIndex).Cells(0).Value.ToString()
Form2.TextBox2.Text = DataDataGridView.Rows(e.RowIndex).Cells(1).Value.ToString()
Form2.TextBox3.Text = DataDataGridView.Rows(e.RowIndex).Cells(2).Value.ToString()
Form2.TextBox4.Text = DataDataGridView.Rows(e.RowIndex).Cells(3).Value.ToString()
Form2.TextBox5.Text = DataDataGridView.Rows(e.RowIndex).Cells(4).Value.ToString()
Form2.TextBox6.Text = DataDataGridView.Rows(e.RowIndex).Cells(5).Value.ToString()
Form2.TextBox7.Text = DataDataGridView.Rows(e.RowIndex).Cells(6).Value.ToString()

What I want is for the user to be able to edit the textbox fields in Form2 and click a submit button to update the DataGridView from the textboxes on Form2.

Thanks in advance.


In the buttons click handler just reverse what you've already done. You wont have an e.RowIndex value but assuming this is the current row of the datagridview you can just do

Dim rowIndex = DataDataGridView.CurrentRow.Index 
DataDataGridView.Rows(rowIndex).Cells(0).Value = Form2.TextBox1.Text 

or easier

DataDataGridView.CurrentRow.Cells(0).Value = Form2.TextBox1.Text