在C#win app中的两列之间的gridview中检查Checkbox。 [英] Checkbox.checked in gridview between two column in C# win app.

查看:75
本文介绍了在C#win app中的两列之间的gridview中检查Checkbox。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网格视图中有两列类型复选框。如何检查column1,column2可以取消选中,反之亦然。

我尝试编写此代码,但我可能遇到很多问题: -

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:-

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

推荐答案

我建​​议使用 BindingSource ,参见详细数据绑定教程

详细数据绑定教程 [ ^ ]



获取或设置值:

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

To get or set the value:
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;
	}
}


这篇关于在C#win app中的两列之间的gridview中检查Checkbox。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆