When a user click on the any record DATAGRIDVIEW1
so the value of Column(0) "ID and column(1) "Name" automatically captured and displayed in textbox
Asked
Active
Viewed 205 times
-1

marc_s
- 732,580
- 175
- 1,330
- 1,459

Adil Numan
- 11
- 5
2 Answers
0
You can use "DataGridView.SelectionChanged Event". This might help you.
private void FormMain_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add(1, "firstname1");
dataGridView1.Rows.Add(2, "firstname2");
dataGridView1.Rows.Add(3, "firstname3");
dataGridView1.MultiSelect = false;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.AllowUserToAddRows = false;
}
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count >= 1)
{
string id = dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString();
string firstname = dataGridView1.SelectedRows[0].Cells["FIRSTNAME"].Value.ToString();
Console.WriteLine(firstname + " with id " + id + " selected");
}
}

Armin
- 576
- 6
- 13
-
ITS VB .NET project buddy sorry I forgot to mention please re-explain it in VB .net – Adil Numan May 06 '20 at 19:42
-
ok but there is another problem 'e.rowindex' is not working in selectionchaged event – Adil Numan May 06 '20 at 19:53
-
How are you adding the event? Use this post to learn how to add events in visual studio. https://stackoverflow.com/questions/1135299/microsoft-visual-studio-and-c-how-to-visually-add-events-to-controls – Armin May 08 '20 at 05:33
0
Well i found the simple code which helped me to get the value from DATAGRIDVIEW
row.Cells(1).Value.ToString
My code for the project
` Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV_SALARY.CellContentClick
Dim rowmemory As String
Dim row As DataGridViewRow = DGV_SALARY.Rows(e.RowIndex)
rowmemory = e.RowIndex
ADD_NAME_TXT.Text = row.Cells(1).Value.ToString
ID_TXT.Text = row.Cells(0).Value.ToString
Call SALARY_NAME_SEARCH_CMD("SALARY_SEARCH_QUERY")
With DGV_SALARY
.Rows(rowmemory).Selected = True
.Refresh()
End With
End Sub`

Adil Numan
- 11
- 5