通过C#.net中的另一个复选框选中和取消选中所有复选框列 [英] Check and uncheck all checkboxcolumns by another checkbox in C#.net

查看:149
本文介绍了通过C#.net中的另一个复选框选中和取消选中所有复选框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......

如何通过datagridview之外的另一个复选框来检查和取消选中datagridview中的Checkbox列.

我必须满足以下三个条件,
1.如果选中了外部复选框,则所有datagridview复选框都被选中,反之亦然.

2.如果选中了所有datagridview复选框,则外部复选框也将被选中.


3.单击外部复选框后,所有复选框均被选中,但如果其中一个未被选中,则其他选项保持不变,但外部复选框将被取消.

我该怎么办?

对于第一个条件,我已经完成了这些操作.

 如果(checkBox1.Checked ==  true )
 {
    for ( int  i =  0 ; i <  datagridview1.RowCount; i ++)
      {
        datagridview1 [ 0 ,i] .Value =  true ;
       }
  } 


但是对于第二个广告的第三个条件,我无法获得适当或精确的解决方案....
请帮忙..

谢谢

解决方案

->注意:wpf和c#
----------------------

对于您的第一个条件(1.如果选中了外部复选框,那么所有datagridview复选框都会被选中,反之亦然)
绑定简短的clr属性

 ///  <  摘要 > 
 /// 复选框全选"
 ///  <  /summary  > 
 私有  isAllSelected;
        公共  IsAllSelected
        {
            获取
            {
                返回 isAllSelected;
            }
            设置
            {
                isAllSelected = ;
                OnPropertyChanged(" );

                // ,用于基于选中的所有选中项进行检查和取消选中
                SelectAllDesignation();
            }
        } 



datagridview databinding创建属性

///<  摘要 <  /summary  > 
        私有DataTable dtDesignation;
        公共数据表指定
        {
            得到
            {
                返回dtDesignation;
            }
            放
            {
                dtDesignation =值;
                OnPropertyChanged("Designation");
            }
        } 



并添加名为IsSelected
的列

DataColumn dtCol = new DataColumn("IsSelected", typeof(Int16));
                dtCol.DefaultValue = 0;
                Designation = objHRmain.FetchDesignation(dtConditions); //fetch your data
                Designation.Columns.Add(dtCol);
                Designation.AcceptChanges();





 ///  <  摘要 > 
 /// 用于全选
 ///  <  /summary  > 
 私有 无效 SelectAllDesignation()
        {
            如果(名称!= && Designation.Rows.Count  0 )
            {
                如果(IsAllSelected ==  1 )
                {
                     foreach (DataRow dr  Designation.Rows中)
                    {
                        dr [" ] = >真 ;
                    }
                }
                其他
                {
                     foreach (DataRow dr  Designation.Rows中)
                    {
                        dr [" ] =  false  ;
                    }
                }
            }
        } 




对于您的第二个和第三个条件(2.如果选中了所有datagridview复选框,那么还将选中外部复选框.并且
3.单击外部复选框后,所有复选框均被选中,但如果其中一个未被选中,则其他选项保持不变,但外部复选框将被取消选中)

创建事件处理程序(如命令,已检查等).
在该事件上,请编写此代码
在这里使用命令绑定

 私有 无效 SelectDetails_Executed(对象发​​件人,ExecutedRoutedEventArgs e)
        {
            如果(例如,参数!= )
            {
                DataRowView drvSelected = e.Parameter  as  DataRowView;
                如果(Convert.ToInt16(drvSelected.Row [" ] ==  0 ;
                    OnPropertyChanged(" );
                }
                其他
                {
                    如果(Designation.AsEnumerable().Count(dr = >  Convert.ToInt16(dr [" ])==  1 )= = Designation.Rows.Count)
                    {
                        isAllSelected =  1 ;
                        OnPropertyChanged(" );
                    }
                    其他
                    {
                        isAllSelected =  0 ;
                        OnPropertyChanged(" );
                    }
                }
            }
        } 




并且不要忘记实现继承INotifyPropertyChanged及其实现

#region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler == null) return;
            handler(this, new PropertyChangedEventArgs(name));
        }
        #endregion


< script type = " > 


(文档).ready(// 用于外部复选框以选中/取消选中网格复选框列


Hi Everyone....

How to check and uncheck Checkboxcolumns in datagridview by an another checkbox which is outside of that datagridview .

i have to fulfill three criteria which are explain below
1.If outside checkbox is checked then all datagridview checkboxes are checked and viceversa .

2.If all datagridview checkboxes are checked then outside checkbox will be also checked .

and
3. After clicking of outside checkbox all checkboxes are checked but if one of them is unchecked then ohers remain same but outside checkbox will be unchecked .

How can i do that ?

for first condition i have done these..

if (checkBox1.Checked == true)
 {
   for (int i = 0; i < datagridview1.RowCount; i++)
      {
        datagridview1[0, i].Value = true;
       }
  }


but for second ad third condition i can not get proper or exact solution....
please help..

Thank You

解决方案

--> note: wpf and c#
----------------------

for your first criteria (1.If outside checkbox is checked then all datagridview checkboxes are checked and viceversa)
bind a short clr property

/// <summary>
        ///for checkbox "select all"
        /// </summary>
        private short isAllSelected;
        public short IsAllSelected
        {
            get
            {
                return isAllSelected;
            }
            set
            {
                isAllSelected = value;
                OnPropertyChanged("IsAllSelected");

                //for checking and unchecking based on select all checked
                SelectAllDesignation();
            }
        }



datagridview databinding create a property

/// <summary>
        /// for designation
        /// </summary>
        private DataTable dtDesignation;
        public DataTable Designation
        {
            get
            {
                return dtDesignation;
            }
            set
            {
                dtDesignation = value;
                OnPropertyChanged("Designation");               
            }
        }



and add a column named as IsSelected

DataColumn dtCol = new DataColumn("IsSelected", typeof(Int16));
                dtCol.DefaultValue = 0;
                Designation = objHRmain.FetchDesignation(dtConditions); //fetch your data
                Designation.Columns.Add(dtCol);
                Designation.AcceptChanges();





/// <summary>
        /// for select all
        /// </summary>
        private void SelectAllDesignation()
        {
            if (Designation != null && Designation.Rows.Count > 0)
            {
                if (IsAllSelected == 1)
                {
                    foreach (DataRow dr in Designation.Rows)
                    {
                        dr["IsSelected"] = true;
                    }
                }
                else
                {
                    foreach (DataRow dr in Designation.Rows)
                    {
                        dr["IsSelected"] = false;
                    }
                }
            }
        }




for your 2nd and 3rd criteria(2.If all datagridview checkboxes are checked then outside checkbox will be also checked .and
3. After clicking of outside checkbox all checkboxes are checked but if one of them is unchecked then ohers remain same but outside checkbox will be unchecked )

create a event handler (like command ,checked etc.. )
on that event write this code
here am used command binding

private void SelectDetails_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Parameter != null)
            {
                DataRowView drvSelected = e.Parameter as DataRowView;
                if (Convert.ToInt16(drvSelected.Row["IsSelected"]) == 0)
                {
                    isAllSelected = 0;
                    OnPropertyChanged("IsAllSelected");
                }
                else
                {
                    if (Designation.AsEnumerable().Count(dr => Convert.ToInt16(dr["IsSelected"]) == 1) == Designation.Rows.Count)
                    {
                        isAllSelected = 1;
                        OnPropertyChanged("IsAllSelected");
                    }
                    else
                    {
                        isAllSelected = 0;
                        OnPropertyChanged("IsAllSelected");
                    }
                }
            }
        }




and also dont forget to implement inheritance INotifyPropertyChanged and its implementation

#region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler == null) return;
            handler(this, new PropertyChangedEventArgs(name));
        }
        #endregion


<script type="text/javascript">


(document).ready(function () { //For outside checkbox to check/uncheck grid checkbox columns


这篇关于通过C#.net中的另一个复选框选中和取消选中所有复选框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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