WPF DevExpress GridControl TableView 全选 和反选 以及获取选中行的数据
2015-07-07 19:19
651 查看
XMAl 文件
<Window x:Class="WpfChart.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="550" Width="955" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">
<Grid>
<dxg:GridControl AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" HorizontalAlignment="Left" Margin="10,59,0,0" Name="gridControl1" VerticalAlignment="Top" Height="445" Width="738" >
<dxg:GridControl.Columns >
<dxg:GridColumn FieldName="IsValid" Header="选中" VisibleIndex="0" AllowEditing="False" AllowSorting="False" >
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Data.IsValid}"
HorizontalAlignment="Center" VerticalAlignment="Center" >
</CheckBox>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn FieldName="ID" VisibleIndex="1" />
<dxg:GridColumn FieldName="Name" VisibleIndex="2" />
<dxg:GridColumn FieldName="StartDateTime" VisibleIndex="3" />
<dxg:GridColumn FieldName="EndDateTime" VisibleIndex="4" />
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView ShowGroupPanel="False" AllowPerPixelScrolling="True" Name="tableView1" ShowTotalSummary="True" AllowFixedGroups="True" AllowColumnFiltering="False" AllowSorting="False" ShowFixedTotalSummary="True" />
</dxg:GridControl.View>
</dxg:GridControl>
<Button Content="全选" Height="23" HorizontalAlignment="Left" Margin="16,28,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="选中行信息" Height="23" HorizontalAlignment="Left" Margin="110,28,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>
</Window>
代码文件
namespace WpfChart
{
/// <summary>
/// Window4.xaml 的交互逻辑
/// </summary>
public partial class Window4 : Window
{
List<Project> data = null;
public Window4()
{
InitializeComponent();
data = new List<Project>();
data.Add(new Project(1, "Apple", DateTime.Now, DateTime.Now, true));
data.Add(new Project(2, "banana", DateTime.Now, DateTime.Now, false));
data.Add(new Project(3, "orange", DateTime.Now, DateTime.Now, true));
data.Add(new Project(4, "peach", DateTime.Now, DateTime.Now, false));
data.Add(new Project(5, "pineapple", DateTime.Now, DateTime.Now, true));
gridControl1.ItemsSource = data;
DataContext = data;
}
/// <summary>
/// 默认全选为false
/// </summary>
bool check = false;
/// <summary>
/// 全选及反选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, RoutedEventArgs e)
{
bool newSelect = !check;
check = !check;
for (int i = 0; i < data.ToList().Count; i++)
{
int rowHandle = this.gridControl1.GetRowHandleByListIndex(i);
gridControl1.SetCellValue(rowHandle, "IsValid", newSelect);
}
}
/// <summary>
/// 获取选中行的信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, RoutedEventArgs e)
{
string name = "";
for (int i = 0; i < data.ToList().Count; i++)
{
int rowHandle = this.gridControl1.GetRowHandleByListIndex(i);
object wholeRowObject = gridControl1.GetRow(rowHandle);
object rowCheck = gridControl1.GetCellValue(rowHandle, "IsValid");
bool ifCheck = (bool)rowCheck;
if (ifCheck)
{
object rowname = gridControl1.GetCellValue(rowHandle, "Name");
name += rowname.ToString() + ",";
}
}
MessageBox.Show(name);
}
}
public class Project
{
static int num = 0;
public int ID { get; set; }
public string Name { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
public bool IsValid { get; set; }
public Project(int id, string name, DateTime startDateTime, DateTime endDateTime ,bool isvalid)
{
ID = id;
Name = name;
StartDateTime = startDateTime;
EndDateTime = endDateTime;
IsValid = isvalid;
}
}
}
<Window x:Class="WpfChart.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="550" Width="955" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">
<Grid>
<dxg:GridControl AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" HorizontalAlignment="Left" Margin="10,59,0,0" Name="gridControl1" VerticalAlignment="Top" Height="445" Width="738" >
<dxg:GridControl.Columns >
<dxg:GridColumn FieldName="IsValid" Header="选中" VisibleIndex="0" AllowEditing="False" AllowSorting="False" >
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Data.IsValid}"
HorizontalAlignment="Center" VerticalAlignment="Center" >
</CheckBox>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn FieldName="ID" VisibleIndex="1" />
<dxg:GridColumn FieldName="Name" VisibleIndex="2" />
<dxg:GridColumn FieldName="StartDateTime" VisibleIndex="3" />
<dxg:GridColumn FieldName="EndDateTime" VisibleIndex="4" />
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView ShowGroupPanel="False" AllowPerPixelScrolling="True" Name="tableView1" ShowTotalSummary="True" AllowFixedGroups="True" AllowColumnFiltering="False" AllowSorting="False" ShowFixedTotalSummary="True" />
</dxg:GridControl.View>
</dxg:GridControl>
<Button Content="全选" Height="23" HorizontalAlignment="Left" Margin="16,28,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="选中行信息" Height="23" HorizontalAlignment="Left" Margin="110,28,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>
</Window>
代码文件
namespace WpfChart
{
/// <summary>
/// Window4.xaml 的交互逻辑
/// </summary>
public partial class Window4 : Window
{
List<Project> data = null;
public Window4()
{
InitializeComponent();
data = new List<Project>();
data.Add(new Project(1, "Apple", DateTime.Now, DateTime.Now, true));
data.Add(new Project(2, "banana", DateTime.Now, DateTime.Now, false));
data.Add(new Project(3, "orange", DateTime.Now, DateTime.Now, true));
data.Add(new Project(4, "peach", DateTime.Now, DateTime.Now, false));
data.Add(new Project(5, "pineapple", DateTime.Now, DateTime.Now, true));
gridControl1.ItemsSource = data;
DataContext = data;
}
/// <summary>
/// 默认全选为false
/// </summary>
bool check = false;
/// <summary>
/// 全选及反选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, RoutedEventArgs e)
{
bool newSelect = !check;
check = !check;
for (int i = 0; i < data.ToList().Count; i++)
{
int rowHandle = this.gridControl1.GetRowHandleByListIndex(i);
gridControl1.SetCellValue(rowHandle, "IsValid", newSelect);
}
}
/// <summary>
/// 获取选中行的信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, RoutedEventArgs e)
{
string name = "";
for (int i = 0; i < data.ToList().Count; i++)
{
int rowHandle = this.gridControl1.GetRowHandleByListIndex(i);
object wholeRowObject = gridControl1.GetRow(rowHandle);
object rowCheck = gridControl1.GetCellValue(rowHandle, "IsValid");
bool ifCheck = (bool)rowCheck;
if (ifCheck)
{
object rowname = gridControl1.GetCellValue(rowHandle, "Name");
name += rowname.ToString() + ",";
}
}
MessageBox.Show(name);
}
}
public class Project
{
static int num = 0;
public int ID { get; set; }
public string Name { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
public bool IsValid { get; set; }
public Project(int id, string name, DateTime startDateTime, DateTime endDateTime ,bool isvalid)
{
ID = id;
Name = name;
StartDateTime = startDateTime;
EndDateTime = endDateTime;
IsValid = isvalid;
}
}
}
相关文章推荐
- Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)
- 【MyBatis框架】查询缓存-二级缓存原理
- 处理getdrawable的方法引起的OOM异常
- Contains Duplicate II
- 建造者模式 Builder Mode
- 毕业了,工作了
- codeforces215 E. Periodical Numbers(数位dp)
- isset,empty,is_null小知识
- 字符串半角全角互转详解
- Centos+mysql+apache(http)+php+wordpress搭建个人站点
- 开发外包注意事项二——iOS APP的开发
- R语言----基于ggplot绘图世界
- AJAX初步学习
- STL之迭代器
- Android入门(32)——第九章 Fragment与Activity通信
- 神州数码802.1x认证协议简析
- Docker学习资料
- hello, world
- 【Mark】如何阅读一本好书:APUE
- 1024Studio官网