In this article I will explain with an example, how to display
DataGridView selected Row in TextBoxes in
Windows FormsForms (WinForms) Application using C# and VB.Net.
The
DataGridView will be assigned
CellMouseClick event handler.
When the
DataGridView Row or Cell is clicked, the Row Index of the clicked
DataGridView Row is determined and the values of the Cells are extracted and displayed in
TextBoxes in
Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
The Form consists of a
DataGridView and three TextBoxes. The
DataGridView has been assigned
CellMouseClick event handler.
Namespaces
You will need to import the following namespace.
C#
VB.Net
Populating the DataGridView
Inside the
Form Load event handler, an object of
DataTable is created.
Then, three columns are added to the
DataTable Columns collection using the
AddRange method.
Inside the AddRange method, an Array of the objects of type DataColumn is specified to which, the name and the optional parameter Data Type i.e. the Type of the column is passed.
Once the schema is ready i.e. all the columns are defined and some rows have been added using the Rows.Add method.
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country", typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
this.dataGridView1.DataSource = dt;
this.dataGridView1.AllowUserToAddRows = false;
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Me.dataGridView1.DataSource = dt
Me.dataGridView1.AllowUserToAddRows = False
End Sub
Displaying DataGridView Selected Row in TextBoxes
When the
DataGridView Row or Cell is clicked, the Row Index of the clicked
DataGridView Row is determined and the values of the Cells are extracted and displayed in
TextBoxes in
Windows Forms (WinForms) Application using C# and VB.Net.
C#
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
txtID.Text = row.Cells[0].Value.ToString();
txtName.Text = row.Cells[1].Value.ToString();
txtCountry.Text = row.Cells[2].Value.ToString();
}
}
VB.Net
Private Sub dataGridView1_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs)Handles dataGridView1.CellMouseClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
txtID.Text = row.Cells(0).Value.ToString
txtName.Text = row.Cells(1).Value.ToString
txtCountry.Text = row.Cells(2).Value.ToString
End If
End Sub
Screenshot
Downloads