Click here to Skip to main content
15,916,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two columns of type checkbox in a grid view. How when i check column1, column2 can become unchecked and vice-versa.
I try to write this code but i have many problems maybe:-
C#
Private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   DataGridViewCheckboxCell Ch1= new DataGridViewCheckboxCel();
   DataGridViewCheckboxCell Ch2= new DataGridViewCheckboxCel();
   
   Ch1 = (DataGridViewCheckboxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];
 Ch2 = (DataGridViewCheckboxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];

   if(ch1.Value == null)
      ch1.Value = false;
   elseif(Ch2.Value != null)
      Ch2.Value = false;

  switch(ch1.Value.ToString())
  {
     Case 'True':
                Ch1.Value = false;
                breake;
     Case 'False':
                if(Convert.ToBoolean(Ch1.Value) == false)
                   Ch2.Value = false;
                break;
  }
 
}


What I have tried:

how i Can Do That Like radio button and thanks
Posted
Updated 29-Aug-16 7:30am
v2
Comments
Maciej Los 29-Aug-16 13:27pm    
What kind of problems?
Check this: http://stackoverflow.com/questions/2885391/datagridview-checkbox-events
MahmoudOmar 30-Aug-16 2:35am    
i have two column in gridview this column it's checkbox like that,

Drivers Name , Absence, vacation
------------ ------- --------
Name1 1 0
Name2 0 1
Name3 1 0
Name4 1 0

i want when i check in Absence the vacation became false , and when i check vacation the absence became false.
How i do that.
please tell me if you don't understand me.
Maciej Los 30-Aug-16 4:25am    
I understand what you're trying to do, but i do not understand what kind of problems do you have.
On the other side, why do you need 2 columns? Vacation is a type of absence. So, why do you want to store that values into 2 different columns?
[EDIT]
Check this: Build a Custom RadioButton Cell and Column for the DataGridView Control
and
DataGridView Custom Column of Type ListView or CheckedListBox
MahmoudOmar 30-Aug-16 4:44am    
First :- Its multiple column not 2 column its just example,
second :- i don't find radio button in grid view column type so i used checkbox

1 solution

I can recommend using a BindingSource, see A Detailed Data Binding Tutorial
A Detailed Data Binding Tutorial[^]

To get or set the value:
C#
private void DataGridView1CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
	switch (this.DataGridView1.Columns[e.ColumnIndex].Name)
	{
		case "Absence":
			bool flag = (bool)this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
			this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value = !flag;
			break;
		case "vacation":
			bool flag = (bool)this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
			this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value = !flag;
			break;
	}
}
 
Share this answer
 
v3
Comments
MahmoudOmar 30-Aug-16 3:09am    
i have two column in gridview this column it's checkbox like that,

Drivers Name , Absence, vacation
------------ ------- --------
Name1 1 0
Name2 0 1
Name3 1 0
Name4 1 0

i want when i check in Absence the vacation became false , and when i check vacation the absence became false.
How i do that.
please tell me if you don't understand me.
RickZeeland 30-Aug-16 5:25am    
See the updated solution !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900