如何取消选中datagrid中的顶级复选框 [英] How to uncheck top level checkbox in datagrid
本文介绍了如何取消选中datagrid中的顶级复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello House,
i正在处理数据网格,其中包含一个级别复选框,如果选中,则还会检查数据网格中的其他复选框(只是相同)方式yahoomail工作),这对我来说很好,我有问题取消选中顶级复选框,如果任何复选框未选中,请我需要你的帮助。这是我的代码:
全局变量:
CheckBox chkbox = 新 CheckBox();
DataTable dt = new DataTable();
private void Form1_Load( object sender,EventArgs e)
{
// CheckBox chkbox = new CheckBox();
dataGridView1.AllowUserToAddRows = false ;
dataGridView1.Columns.Clear();
DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
colCB.Name = chkcol;
colCB.HeaderText = ;
colCB.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns.Add(colCB);
Rectangle rect = this .dataGridView1.GetCellDisplayRectangle( 0 , - 1, true );
chkbox.Size = new 大小( 18 , 18 );
rect.Offset( 43 , 2 );
chkbox.Location = rect.Location;
chkbox.CheckedChanged + = chkBoxChange;
此 .dataGridView1.Controls.Add(chkbox);
DataRow dr = default (DataRow);
// 声明列名
dt.Columns.Add ( eno);
dt.Columns.Add( empname);
dt.Columns.Add( sal);
// 创建包含数据的行
dr = dt。 NewRow();
dr [ eno] = 101 跨度>;
dr [ empname] = test1;
dr [ sal] = 9000 跨度>;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr [ eno] = 102 跨度>;
dr [ empname] = test2;
dr [ sal] = 15000 跨度>;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr [ eno] = 103 跨度>;
dr [ empname] = test3;
dr [ sal] = 20000 跨度>;
dt.Rows.Add(dr);
// 将数据表数据绑定到datagridview
dataGridView1 .DataSource = dt;
}
private void dataGridView1_CellContentClick( object sender,DataGridViewCellEventArgs e)
{
double k = 0 。 0 ;
if (e.ColumnIndex == 0 )
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
for ( int i = 0 ; i < = dataGridView1.RowCount - 1 ; i ++)
{
// chkcol是复选框列
if (Convert.ToBoolean(dataGridView1.Rows [i] .Cells [ chkcol ] .Value)== true )
{
k = k + Convert.ToDouble(dataGridView1.Rows [i] .Cells [ 3 ]。值); // 3是工资栏
}
}
// 这一个是总显示文本框
// label1.Text = k.ToString();
}
}
any我将不胜感激。
解决方案
查看这篇文章。我认为这就是你要找的东西
切换DataGridView列中所有CheckBox的状态
Hello House,
i am working with datagrid that has a level checkbox of which if it is checked others checkbox in the datagrid will also be checked (just the same way yahoomail works), that is working fine for me, i am having issues unchecking the top level checkbox if any of the checkboxes is unchecked, please i need your assistance. here is my code:
Global variables:
CheckBox chkbox=new CheckBox( );
DataTable dt = new DataTable( );
private void Form1_Load(object sender, EventArgs e)
{
//CheckBox chkbox=new CheckBox( );
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Clear( );
DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn( );
colCB.Name = "chkcol";
colCB.HeaderText = "";
colCB.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns.Add( colCB );
Rectangle rect = this.dataGridView1.GetCellDisplayRectangle( 0, -1, true );
chkbox.Size = new Size( 18, 18 );
rect.Offset( 43, 2 );
chkbox.Location = rect.Location;
chkbox.CheckedChanged += chkBoxChange;
this.dataGridView1.Controls.Add( chkbox );
DataRow dr = default( DataRow );
//Declare Column names
dt.Columns.Add( "eno" );
dt.Columns.Add( "empname" );
dt.Columns.Add( "sal" );
//Create rows with data
dr = dt.NewRow( );
dr["eno"] = 101;
dr["empname"] = "test1";
dr["sal"] = 9000;
dt.Rows.Add( dr );
dr = dt.NewRow( );
dr["eno"] = 102;
dr["empname"] = "test2";
dr["sal"] = 15000;
dt.Rows.Add( dr );
dr = dt.NewRow( );
dr["eno"] = 103;
dr["empname"] = "test3";
dr["sal"] = 20000;
dt.Rows.Add( dr );
//Bind that datatable data into the datagridview
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
double k = 0.0;
if ( e.ColumnIndex == 0 )
{
dataGridView1.CommitEdit( DataGridViewDataErrorContexts.Commit );
for ( int i = 0 ; i <= dataGridView1.RowCount - 1 ; i++ )
{
//chkcol is checkbox column
if ( Convert.ToBoolean( dataGridView1.Rows[i].Cells["chkcol"].Value ) == true )
{
k = k + Convert.ToDouble( dataGridView1.Rows[i].Cells[3].Value ); //3 is salary column
}
}
//This one is total display textbox
//label1.Text = k.ToString( );
}
}
any assistance will be appreciated thanks.
解决方案
Check this Article.I thinks this is what you are looking for
Toggling the States of all CheckBoxes Inside a DataGridView Column
这篇关于如何取消选中datagrid中的顶级复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文