使用datagrid做全选全不选(拿出你得Codeing)

灬浪子灬 2011-08-29 03:42:46
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Infragistics.Samples.WPF.xamFeatureBrowser.Samples.XamDataGrid
{
public partial class CheckBoxInRecordSelectors_Samp : Page
{
public CheckBoxInRecordSelectors_Samp()
{
InitializeComponent();

List<PersonViewModel> members =
(from p in Person.GetPeople()
select new PersonViewModel(p))
.ToList();

base.DataContext = new CommunityViewModel(members);
}

void OnShowRecords(object sender, RoutedEventArgs e)
{
List<string> checkedNames = new List<string>();
List<string> uncheckedNames = new List<string>();

var community = base.DataContext as CommunityViewModel;
foreach (PersonViewModel p in community.Members)
{
if (p.IsChecked)
checkedNames.Add(p.LastName);
else
uncheckedNames.Add(p.LastName);
}

checkedNames.Sort();
uncheckedNames.Sort();

StringBuilder sb = new StringBuilder();

sb.AppendLine(Strings.ControlComposition_CheckBoxInRecordSelectors_MessageBox_Checked);
foreach (string s in checkedNames)
sb.AppendLine(s);

sb.AppendLine();

sb.AppendLine(Strings.ControlComposition_CheckBoxInRecordSelectors_MessageBox_Unchecked);
foreach (string s in uncheckedNames)
sb.AppendLine(s);

string msg = sb.ToString();
string caption = Strings.ControlComposition_CheckBoxInRecordSelectors_MessageBox_Caption;
MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Information);
}

#region Person [nested class]

/// <summary>
/// A simple data object that stores information about a person.
/// </summary>
public class Person
{
public static Person[] GetPeople()
{
return new Person[]
{
new Person("Jane", "Smith", 23),
new Person("Mike", "Wheeler", 45),
new Person("Ned", "Ableton", 67),
new Person("Patrick", "McCort", 12),
new Person("Ravi", "Patel", 28),
new Person("Wallace", "Barrington", 42),
};
}

public Person(string firstName, string lastName, int age)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
}

public int Age { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
}

#endregion // Person [nested class]

#region PersonViewModel [nested class]

/// <summary>
/// A presentation-friendly wrapper for the Person class, which has support for being 'checked.'
/// </summary>
public class PersonViewModel : INotifyPropertyChanged
{
readonly Person _person;
bool _isChecked;

public PersonViewModel(Person person)
{
_person = person;
}

public bool IsChecked
{
get { return _isChecked; }
set
{
if (value == _isChecked)
return;

_isChecked = value;

this.OnPropertyChanged("IsChecked");
}
}

public int Age { get { return _person.Age; } }
public string FirstName { get { return _person.FirstName; } }
public string LastName { get { return _person.LastName; } }

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
}

#endregion // INotifyPropertyChanged Members
}

#endregion // PersonViewModel [nested class]

#region CommunityViewModel [nested class]

/// <summary>
/// A presentation-friendly class that contains a list of PersonViewModel objects
/// and manages an aggregated check state for the group, via the get/set property
/// called AllMembersAreChecked.
/// </summary>
public class CommunityViewModel : INotifyPropertyChanged
{
public CommunityViewModel(List<PersonViewModel> members)
{
this.Members = members;

foreach (PersonViewModel member in members)
member.PropertyChanged += (sender, e) =>
{
// Raising PropertyChanged for the AllMembersAreChecked
// property causes the data binding system to query its
// getter for the new value.
if (e.PropertyName == "IsChecked")
this.OnPropertyChanged("AllMembersAreChecked");
};
}

public List<PersonViewModel> Members { get; private set; }

public bool? AllMembersAreChecked
{
get
{
// Determine if all members have the same
// value for the IsChecked property.
bool? value = null;
for (int i = 0; i < this.Members.Count; ++i)
{
if (i == 0)
{
value = this.Members[0].IsChecked;
}
else if (value != this.Members[i].IsChecked)
{
value = null;
break;
}
}

return value;
}
set
{
if (value == null)
return;

foreach (PersonViewModel member in this.Members)
member.IsChecked = value.Value;
}
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
}

#endregion // INotifyPropertyChanged Members
}

#endregion // CommunityViewModel [nested class]
}

public class CheckBoxInRecordSelectors_Samp_Root
: Infragistics.Samples.WPF.xamFeatureBrowser.Controls.LocalizationRoot
{
public string Button_ShowRecords
{
get { return Strings.ControlComposition_CheckBoxInRecordSelectors_Button_ShowRecords; }
}
}
}

这个是我看的一个例子 可以实现全选全不选 但是Header好像不能自定义!
有没有简单点得实现方法!假的我也有!
假的就算了!希望大家能交流下!
...全文
277 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
guoyanhong1111 2011-08-30
  • 打赏
  • 举报
回复
建议楼主发帖的时候,是源代码的,要选取你相应的格式,要不我们会看得头昏的!
auogsy 2011-08-30
  • 打赏
  • 举报
回复
没弄过,学习
liufeihardwork 2011-08-29
  • 打赏
  • 举报
回复
还是用js 或Jquery 吧,方便好用。
灬浪子灬 2011-08-29
  • 打赏
  • 举报
回复
WPF中datagrid实现全选全不选就这么的发杂吗?
萧炎 2011-08-29
  • 打赏
  • 举报
回复
lz
我有winfrom实现全选反选的demo
QQ453367672
潘少博 2011-08-29
  • 打赏
  • 举报
回复
好长好长
赢在执行 2011-08-29
  • 打赏
  • 举报
回复

/// <summary>
/// 全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCheckAll_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < this.dgvStock.Rows.Count; i++)
{
DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)this.dgvStock.Rows[i].Cells["ISCheck"];
checkcell.Value = true;
}
}
catch (Exception exp)
{
MessageHelper.ShowInfo(this, exp.Message);
}
}
/// <summary>
/// 不全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCheckCancel_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < this.dgvStock.Rows.Count; i++)
{
DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)this.dgvStock.Rows[i].Cells["ISCheck"];
checkcell.Value = false;
}
}
catch (Exception exp)
{
MessageHelper.ShowInfo(this, exp.Message);
}
}
/// <summary>
/// 单选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvStock_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == -1 || e.RowIndex == -1) return;
DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)this.dgvStock.Rows[e.RowIndex].Cells["ISCheck"];
Boolean flag = Convert.ToBoolean(checkcell.Value);
if (flag)
{
checkcell.Value = false;
}
else
{
checkcell.Value = true;
}
}
catch (Exception exp)
{
MessageHelper.ShowInfo(this, exp.Message);
}
}
LMAOhuaNL 2011-08-29
  • 打赏
  • 举报
回复
private void checkedAll_CheckedChanged(object sender, System.EventArgs e)
{
// 选中全选
if(checkedAll.Checked.Equals(true))
{
foreach(DataGridItem dgi in dtgShowData.Items)
{
CheckBox cb1 = (CheckBox)dgi.FindControl("checkBox1");
// 选中
cb1.Checked = true;
}
}
// 取消全选
if(checkedAll.Checked.Equals(false))
{
foreach(DataGridItem dgi in dtgShowData.Items)
{
CheckBox cb2 = (CheckBox)dgi.FindControl("checkBox1");
// 取消选中
cb2.Checked = false;
}
}
}


<!--页面:Datagrid外位置增加一个复选框asp:CheckBox。选中则为全选,取消选中则为全不选。-->
<asp:CheckBox Runat="server" ID="checkedAll" AutoPostBack="True" Text="全选/全不选"></asp:CheckBox>




111,112

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧