关闭

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

1620人阅读 评论(0) 收藏 举报
分类:

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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10039次
    • 积分:143
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档