Hello ,
If I understand the question correct,
when you check one of the checkBox the other checkBox
in the same DataGridView row should be unchecked.
For that you need the number of Row and Column where is the user
unchecked or checked the checkBox.
If it is the right checkBox the other should be easy to uncheck or check.
So,
all you need is to (create code for..)
process the event called CellContentClick when you will find the
number of Row and Column of the cell in
the DataGridView in wich the user have checked the checkBox.
Afther that you will easy uncheck or check all the other cells in the same row.
Example :
Create DataGridView1 with 3 columns and maximum 10 Rows,
first column initial state is checked and all the other are unchecked.
When you uncheck first column checkBox the other change state to checked,
and when you check first column checkBox the other change state to unchecked.
Program.cs
using System;
using System.Windows.Forms;
namespace Data_grid_view_example
{
internal sealed class Program
{
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
MainForm.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Data_grid_view_example
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
DataGridView1SetValue();
}
void DataGridView1SetValue()
{
int Row = 0;
int Column = 0;
dataGridView1.RowCount = 10;
while (Row < 10)
{
dataGridView1[Column,Row].Value = true;
dataGridView1[Column+1,Row].Value = false;
dataGridView1[Column+2,Row].Value = false;
Row = Row + 1;
}
dataGridView1.Refresh();
}
void DataGridView1CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int Row = 0;
int Column = 0;
string state = " ";
Row = dataGridView1.CurrentRow.Index;
Column = dataGridView1.CurrentCell.ColumnIndex;
if (Column == 0)
{
state = dataGridView1[Column,Row].Value.ToString();
if (state == "True")
{
dataGridView1[Column,Row].Value = false;
dataGridView1[Column+1,Row].Value = true;
dataGridView1[Column+2,Row].Value = true;
}
else
{
dataGridView1[Column,Row].Value = true;
dataGridView1[Column+1,Row].Value = false;
dataGridView1[Column+2,Row].Value = false;
}
}
dataGridView1.Refresh();
}
}
}
MainForm.Designer.cs
namespace Data_grid_view_example
{
partial class MainForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2,
this.Column3});
this.dataGridView1.Location = new System.Drawing.Point(10, 23);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(344, 322);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.DataGridView1CellContentClick);
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle1.NullValue = "False";
this.Column1.DefaultCellStyle = dataGridViewCellStyle1;
this.Column1.HeaderText = "Column1";
this.Column1.Name = "Column1";
this.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.Column1.Width = 101;
this.Column2.HeaderText = "Column2";
this.Column2.Name = "Column2";
this.Column2.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.Column2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.Column3.HeaderText = "Column3";
this.Column3.Name = "Column3";
this.Column3.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.Column3.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(455, 489);
this.Controls.Add(this.dataGridView1);
this.Name = "MainForm";
this.Text = "Data grid view example";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
private System.Windows.Forms.DataGridViewCheckBoxColumn Column3;
private System.Windows.Forms.DataGridViewCheckBoxColumn Column2;
private System.Windows.Forms.DataGridViewCheckBoxColumn Column1;
public System.Windows.Forms.DataGridView dataGridView1;
}
}
All the best
Perić Željko