In this article I will explain with an example, how to allow only one selected row in DataGridView in Windows Forms (WinForms) Application using C# and VB.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Allow only one Selected Row in DataGridView using C# and VB.Net
 
I have already inserted few records in the table.
Allow only one Selected Row in DataGridView using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Form Design

The Form consists of following control:
DataGridView – For displaying data.
Allow only one Selected Row in DataGridView using C# and VB.Net
 
The DataGridView has been assigned with following property.

Properties

MultiSelect – It allows selection of multiple rows.
Disable multiple selection of DataGridView Rows using C# and VB.Net
 
The above property can be set through Code-Behind in following way:
C#
datagridview1.MultiSelect = false;
 
VB.Net
datagridview1.MultiSelect = False
 
 

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
 
 

Disable multiple selection of DataGridView Rows in Windows Forms

Inside the Form Load event handler, first the connection is read from App.Config file.
Note: For more details on how to read connection string from App.Config file, please refer my article Read (Get) Connection String from App.Config file using C# and VB.Net.
 
Then, the records are fetched from the Customers Table of SQL Server database.
Finally, DataTable is assigned to the DataSource property of DataGridView and MultiSelect property of DataGridView is set to FALSE.
C#
private void Form1_Load(object sender, EventArgs e)
{
    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);
                datagridview1.DataSource = dt;
            }
        }
    }
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    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 sdr As SqlDataAdapter = New SqlDataAdapter(sql, con)
            Using dt As DataTable = New DataTable()
                sda.Fill(dt);
                datagridview1.DataSource = dt
            End Using
        End Using
    End Using
End Sub
 
 

Screenshot

Allow only one Selected Row in DataGridView using C# and VB.Net
 
 

Downloads