Hi satabeach,
Check the below example.
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.BindDataGridView();
}
private void BindDataGridView()
{
dgvDetails.AllowUserToAddRows = false;
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Course"));
dt.Rows.Add("Diploma in Information Technology");
dt.Rows.Add("Diploma in Bussiness Administration");
dgvDetails.DataSource = dt;
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "First Choice";
checkBoxColumn.Width = 80;
checkBoxColumn.Name = "cbCheck1";
dgvDetails.Columns.Insert(1, checkBoxColumn);
DataGridViewCheckBoxColumn checkBoxColumn2 = new DataGridViewCheckBoxColumn();
checkBoxColumn2.HeaderText = "Second Choice";
checkBoxColumn2.Width = 100;
checkBoxColumn2.Name = "cbCheck2";
dgvDetails.Columns.Insert(2, checkBoxColumn2);
dgvDetails.CellContentClick += new DataGridViewCellEventHandler(dgvDetails_CellClick);
}
private void dgvDetails_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row1 = dgvDetails.Rows[0];
DataGridViewRow row2 = dgvDetails.Rows[1];
if (e.RowIndex == 0 && e.ColumnIndex == 1)
{
row1.Cells["cbCheck2"].Value = !Convert.ToBoolean(row1.Cells["cbCheck1"].EditedFormattedValue);
row2.Cells["cbCheck1"].Value = !Convert.ToBoolean(row1.Cells["cbCheck1"].EditedFormattedValue);
row2.Cells["cbCheck2"].Value = Convert.ToBoolean(row1.Cells["cbCheck1"].EditedFormattedValue);
}
if (e.RowIndex == 0 && e.ColumnIndex == 2)
{
row1.Cells["cbCheck1"].Value = !Convert.ToBoolean(row1.Cells["cbCheck2"].EditedFormattedValue);
row2.Cells["cbCheck1"].Value = Convert.ToBoolean(row1.Cells["cbCheck2"].EditedFormattedValue);
row2.Cells["cbCheck2"].Value = !Convert.ToBoolean(row1.Cells["cbCheck2"].EditedFormattedValue);
}
if (e.RowIndex == 1 && e.ColumnIndex == 1)
{
row2.Cells["cbCheck2"].Value = !Convert.ToBoolean(row2.Cells["cbCheck1"].EditedFormattedValue);
row1.Cells["cbCheck1"].Value = !Convert.ToBoolean(row2.Cells["cbCheck1"].EditedFormattedValue);
row1.Cells["cbCheck2"].Value = Convert.ToBoolean(row2.Cells["cbCheck1"].EditedFormattedValue);
}
if (e.RowIndex == 1 && e.ColumnIndex == 2)
{
row2.Cells["cbCheck1"].Value = !Convert.ToBoolean(row2.Cells["cbCheck2"].EditedFormattedValue);
row1.Cells["cbCheck1"].Value = Convert.ToBoolean(row2.Cells["cbCheck2"].EditedFormattedValue);
row1.Cells["cbCheck2"].Value = !Convert.ToBoolean(row2.Cells["cbCheck2"].EditedFormattedValue);
}
}
}
VB.Net
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BindDataGridView()
End Sub
Private Sub BindDataGridView()
dgvDetails.AllowUserToAddRows = False
Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("Course"))
dt.Rows.Add("Diploma in Information Technology")
dt.Rows.Add("Diploma in Bussiness Administration")
dgvDetails.DataSource = dt
Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = "First Choice"
checkBoxColumn.Width = 80
checkBoxColumn.Name = "cbCheck1"
dgvDetails.Columns.Insert(1, checkBoxColumn)
Dim checkBoxColumn2 As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
checkBoxColumn2.HeaderText = "Second Choice"
checkBoxColumn2.Width = 100
checkBoxColumn2.Name = "cbCheck2"
dgvDetails.Columns.Insert(2, checkBoxColumn2)
AddHandler dgvDetails.CellContentClick, AddressOf dgvDetails_CellClick
End Sub
Private Sub dgvDetails_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
Dim row1 As DataGridViewRow = dgvDetails.Rows(0)
Dim row2 As DataGridViewRow = dgvDetails.Rows(1)
If e.RowIndex = 0 AndAlso e.ColumnIndex = 1 Then
row1.Cells("cbCheck2").Value = Not Convert.ToBoolean(row1.Cells("cbCheck1").EditedFormattedValue)
row2.Cells("cbCheck1").Value = Not Convert.ToBoolean(row1.Cells("cbCheck1").EditedFormattedValue)
row2.Cells("cbCheck2").Value = Convert.ToBoolean(row1.Cells("cbCheck1").EditedFormattedValue)
End If
If e.RowIndex = 0 AndAlso e.ColumnIndex = 2 Then
row1.Cells("cbCheck1").Value = Not Convert.ToBoolean(row1.Cells("cbCheck2").EditedFormattedValue)
row2.Cells("cbCheck1").Value = Convert.ToBoolean(row1.Cells("cbCheck2").EditedFormattedValue)
row2.Cells("cbCheck2").Value = Not Convert.ToBoolean(row1.Cells("cbCheck2").EditedFormattedValue)
End If
If e.RowIndex = 1 AndAlso e.ColumnIndex = 1 Then
row2.Cells("cbCheck2").Value = Not Convert.ToBoolean(row2.Cells("cbCheck1").EditedFormattedValue)
row1.Cells("cbCheck1").Value = Not Convert.ToBoolean(row2.Cells("cbCheck1").EditedFormattedValue)
row1.Cells("cbCheck2").Value = Convert.ToBoolean(row2.Cells("cbCheck1").EditedFormattedValue)
End If
If e.RowIndex = 1 AndAlso e.ColumnIndex = 2 Then
row2.Cells("cbCheck1").Value = Not Convert.ToBoolean(row2.Cells("cbCheck2").EditedFormattedValue)
row1.Cells("cbCheck1").Value = Convert.ToBoolean(row2.Cells("cbCheck2").EditedFormattedValue)
row1.Cells("cbCheck2").Value = Not Convert.ToBoolean(row2.Cells("cbCheck2").EditedFormattedValue)
End If
End Sub
End Class
Screenshot