使用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好像不能自定义!
有没有简单点得实现方法!假的我也有!
假的就算了!希望大家能交流下!
...全文
256 8 打赏 收藏 转发到动态 举报
写回复
用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>




jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面。开发者不需要编写复杂的javascript,也不需要对css样式有深入的了解,开发者需要了解的只有一些简单的html标签 1.3.6更新 Bug treegrid: getChecked方法不能返回正确的行. fixed. tree: 异步树,在onlyLeafCheck:true时复选框不显示正确. fixed. Improvement treegrid:继承datagrid组件所有的selecting和checking方法。 linkbutton:图标对齐方式,支持值:'top','bottom','left','right'。 linkbutton:添加"size"属性,支持值:'small','large'。 linkbutton:添加的onClick事件。 menubutton:添加"menuAlign"属性,允许用户设置顶级菜单对齐。 combo:添加"panelAlign"属性,支持值:'left','right'。 calendar:"formatter"、"styler"和"validator"选项可用于自定义日历日期。 calendar:添加的onChange事件。 panel:添加"method","queryParams"和"loader"属性。 panel:添加"onLoadError"事件。 datagrid:添加"onBeginEdit"事件。 datagrid:添加"onEndEdit"事件。 datagrid:添加"sort"方法和"onBeforeSortColumn"事件。 datagrid:"combogrid"编辑器集成到datagriddatagrid:添加"ctrlSelect"属性,允许使用ctrl+click 多选 slider:添加"converter"选项,允许用户决定如何将一个值转换为滑块的位置或滑块位置值。 searchbox:添加"disabled"属性。 searchbox:添加"disabled","enable","clear","reset"方法。 spinner:添加"readonly"属性、"readonly"方法和"onChange事件。

110,534

社区成员

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

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

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