简体   繁体   中英

Getting Datagridview value

Using VB.Net,

In my application, am using datagridview, when i clicking the particular row, that rows value should be appear in the textbox.

So the code should be come under the DataGrid3_CellMouseDoubleClick Event.

How to dispaly a rows value in the textbox.

For Example

3 rows means - 3 rows values should display in 3 textbox.

vb6 code

Private Sub datagrid1_DblClick()

    textbox1 = datagrid1.SelectedItem.SubItems(1)
    textbox2 = datagrid1.SelectedItem.SubItems(2)
    textbox3 = datagrid1.SelectedItem.SubItems(3)
End Sub

How to write a code in vb.net by getting datagrid row values.

Need VB.Net sample code Help

Try this ->

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
TextBox1.Text = DataGridView1.Item(0, i).Value
TextBox2.Text = DataGridView1.Item(1, i).Value
TextBox3.Text = DataGridView1.Item(2, i).Value
TextBox4.Text = DataGridView1.Item(3, i).Value
End Sub

Here i is the RowIndex of the selected row for DataGridView1.Item(0, i), and the number is the cell index.

You will have to loop through the rows and cells of your datagridview in order to get the values.

Something like this:

if (datagrid1.SelectedRows.Count > 0)
{
     for (int i = 0; i < datagrid1.SelectedRows.Count; i++)
     {
          DataGridViewRow gridRow = datagrid1.SelectedRows[i];
          textbox1 = gridRow.Cells[0].Value;
          textbox2 = gridRow.Cells[1].Value;
          textbox3 = gridRow.Cells[2].Value;
     }
}

I assume you want the selected rows only. If not should be easy to just loop through all of them. Also I am not sure how to convert it to VB.NET but am sure you can see what I am trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM