Hi gokuldas,
Refer below sample.
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
public Form1()
{
InitializeComponent();
this.BindGrid();
}
private void BindGrid()
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT Id,UserName,UserStatus FROM Status", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.ReadOnly = true;
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
dataGridView1.Columns.Insert(0, checkBoxColumn);
dataGridView1.Columns[3].Visible = false;
}
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell oCell = row.Cells["checkBoxColumn"] as DataGridViewCheckBoxCell;
bool bChecked = (Convert.ToBoolean(row.Cells[3].Value));
if (bChecked)
{
row.Cells["checkBoxColumn"].Value = true;
}
else
{
row.Cells["checkBoxColumn"].Value = false;
}
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.BindGrid()
End Sub
Private Sub BindGrid()
Dim con As SqlConnection = New SqlConnection(ConnectionString)
Dim cmd As SqlCommand = New SqlCommand("SELECT Id,UserName,UserStatus FROM Status", con)
cmd.CommandType = CommandType.Text
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
dataGridView1.DataSource = dt
dataGridView1.[ReadOnly] = True
Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
dataGridView1.Columns.Insert(0, checkBoxColumn)
dataGridView1.Columns(3).Visible = False
End Sub
Private Sub dataGridView1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
For Each row As DataGridViewRow In dataGridView1.Rows
Dim oCell As DataGridViewCheckBoxCell = TryCast(row.Cells("checkBoxColumn"), DataGridViewCheckBoxCell)
Dim bChecked As Boolean = (Convert.ToBoolean(row.Cells(3).Value))
If bChecked Then
row.Cells("checkBoxColumn").Value = True
Else
row.Cells("checkBoxColumn").Value = False
End If
Next
End Sub
Screenshot