WPF DevExpress GridControl TableView 全选 和反选 以及获取选中行的数据

原创 2015年07月07日 19:19:52

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;
        }
    }
}


相关文章推荐

GridControl 删除选中行

GridControl 删除选中行,Winfrom,WPF
  • hotmee
  • hotmee
  • 2015年12月09日 09:40
  • 2006

DEV 中 gridControl1添加checkbox 列

直接在 gridControl1.DataSource要绑定的表里dt增加一个BOOL列dt.Columns.Add("isCheck", System.Type.GetType("System.Bo...

DevExpress GridControl使用教程:添加checkbox复选框

一、如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二、如何新增一条记录 (1)、gridView.A...

gridcontrol中添加checkbox复选框

添加一列,FieldName为 "check",将ColumnEdit 设置为 复选框 样式。gridview1 editable设置为true   将要绑定的DataTable添加列 "chec...

Dev GridControl 控件checkBox复选框,选中多行(三)

一、GridContorl显示chekbox 设置MultiSelectMode = CheckBoxRowSelect; MultiSelect = True;  二、获取checkBox选中行 ...
  • yb305
  • yb305
  • 2016年08月19日 11:41
  • 3863

GridView实现多选,全选,反选等功能

先看看效果 我这里用到了数据库来储存选择的数据,数据库的简单实用我在前面的博客写过了,这里就不多说了,直接贴代码数据库操作类的代码package com.duanlian.gridviewmulti...

GridControl 获取选中行的数量

MessageBox.Show(BANDGW.SelectedRowsCount.ToString()); BANDGW,表示GridControl 控件内的子控件名称,普通的GridContro...
  • hotmee
  • hotmee
  • 2015年11月18日 13:12
  • 900

wpf devexpress gridcontrol分组统计功能

gridcontrol可以根据某列的值进行分组,并可以统计改组的数据情况 支持多级分组                                         GroupIndex="0...

Devexpress GridControl 如何合并单元格

XML代码如下:
  • hotmee
  • hotmee
  • 2016年12月29日 14:16
  • 1283

C# Dev GridView 获取选中分组下的所有数据行

现在要
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:WPF DevExpress GridControl TableView 全选 和反选 以及获取选中行的数据
举报原因:
原因补充:

(最多只允许输入30个字)