In this article I will explain with an example, how to select DataGridView row in Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
The Form consists of following controls:
DataGridView – For displaying data.
Button – For displaying the row details.
The Button has been assigned with a Click event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Linq
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Binding DataGridView in C# and VB.Net
Inside the Form Load event handler, the BindGrid method is called.
BindGrid
Inside the BindGrid method, records are fetched from Customers table and DataGridView is populated.
C#
private void Form1_Load(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
string sql = "SELECT CustomerId, Name, Country FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Set AutoGenerateColumns False.
dataGridView1.AutoGenerateColumns = false;
//Set Columns Count.
dataGridView1.ColumnCount = 3;
//Add Columns.
dataGridView1.Columns[0].Name = "CustomerId";
dataGridView1.Columns[0].HeaderText = "Customer Id";
dataGridView1.Columns[0].DataPropertyName = "CustomerId";
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].DataPropertyName = "Name";
dataGridView1.Columns[2].Name = "Country";
dataGridView1.Columns[2].HeaderText = "Country";
dataGridView1.Columns[2].DataPropertyName = "Country";
dataGridView1.DataSource = dt;
}
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Me.BindGrid()
End Sub
Private Sub BindGrid()
Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
'Set AutoGenerateColumns False.
dataGridView1.AutoGenerateColumns = False
'Set Columns Count.
dataGridView1.ColumnCount = 3
'Add Columns.
dataGridView1.Columns(0).Name = "CustomerId"
dataGridView1.Columns(0).HeaderText = "Customer Id"
dataGridView1.Columns(0).DataPropertyName = "CustomerId"
dataGridView1.Columns(1).HeaderText = "Name"
dataGridView1.Columns(1).Name = "Name"
dataGridView1.Columns(1).DataPropertyName = "Name"
dataGridView1.Columns(2).Name = "Country"
dataGridView1.Columns(2).HeaderText = "Country"
dataGridView1.Columns(2).DataPropertyName = "Country"
dataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub
Selecting DataGridView Row using C# and VB.Net
When the Get Button is clicked, a FOR loop is executed over the selected rows and using Append method the details of that row is added to StringBuilder class object.
Finally, the content of the StringBuilder object is displayed using Message Box.
C#
private void OnCheck(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("Customer Details:");
sb.Append(string.Concat(Enumerable.Repeat(Environment.NewLine, 2)));
for (int i = 0; i < selectedRowCount; i++)
{
sb.Append("Customer Id: ");
sb.Append(dataGridView1.SelectedRows[i].Cells[0].Value.ToString());
sb.Append(Environment.NewLine);
sb.Append("Name: ");
sb.Append(dataGridView1.SelectedRows[i].Cells[1].Value.ToString());
sb.Append(Environment.NewLine);
sb.Append("Country: ");
sb.Append(dataGridView1.SelectedRows[i].Cells[2].Value.ToString());
sb.Append(string.Concat(Enumerable.Repeat(Environment.NewLine, 2)));
}
MessageBox.Show(sb.ToString());
}
VB.Net
Private Sub OnCheck(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click
Dim sb As StringBuilder = New StringBuilder()
sb.Append("Customer Details:")
sb.Append(String.Concat(Enumerable.Repeat(Environment.NewLine, 2)))
For i As Integer = 0 To selectedRowCount - 1
sb.Append("Customer Id: ")
sb.Append(dataGridView1.SelectedRows(i).Cells(0).Value.ToString())
sb.Append(Environment.NewLine)
sb.Append("Name: ")
sb.Append(dataGridView1.SelectedRows(i).Cells(1).Value.ToString())
sb.Append(Environment.NewLine)
sb.Append("Country: ")
sb.Append(dataGridView1.SelectedRows(i).Cells(2).Value.ToString())
sb.Append(String.Concat(Enumerable.Repeat(Environment.NewLine, 2)))
Next
MessageBox.Show(sb.ToString())
End Sub
Screenshot
Downloads