In this article I will explain with an example, how to check if DataGridView row is selected or not in Windows Forms (WinForms) Application with C# and VB.Net.
Form Design
The Form consists of following controls:
DataGridView – For displaying data.
Button – For deleting the row.
The Button has been assigned with a Click event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
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
Checking whether DataGridView Rows selected or not using C# and VB.Net
When the Check Button is clicked, a variable is initialized where the count of selected row is stored.
Then, a check is performed if count is greater than 0 then, row count is displayed if not then, the appropriate message is displayed using MessageBox class.
C#
private void OnCheck(object sender, EventArgs e)
{
int selectedRowCount = dataGridView1.SelectedRows.Count;
if (selectedRowCount > 0)
{
MessageBox.Show("Selected row count: " + selectedRowCount.ToString());
}
else
{
MessageBox.Show("Please select a row.");
}
}
VB.Net
Private Sub OnCheck(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click
Dim selectedRowCount As Integer = dataGridView1.SelectedRows.Count
If selectedRowCount > 0 Then
MessageBox.Show("Selected row count: " & selectedRowCount.ToString())
Else
MessageBox.Show("Please select a row.")
End If
End Sub
Screenshot
Downloads