--> 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
private short isAllSelected;
public short IsAllSelected
{
get
{
return isAllSelected;
}
set
{
isAllSelected = value;
OnPropertyChanged("IsAllSelected");
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);
Designation.Columns.Add(dtCol);
Designation.AcceptChanges();
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