靠SelectionChanged事件撑起来的Gridview CheckBox&MultiSelection 全选、反选\选择事件

时间:2021-1-12 作者:WilliamChan

启用多选列

gridView1.OptionsSelection.MultiSelect = true;
gridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect;

选中清除

gridView1.UnselectRow(rowHandle);
gridView1.SelectRow();
gridView1.UnselectRow(rowHandle);
gridView1.SelectAll();
gridView1.OptionsSelection.ResetSelectionClickOutsideCheckboxSelector = true;
gridView1.UnselectRow(rowHandle);
gridView1.ClearSelection();

获取选中的行

GridView_SelectionChanged事件

void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case CollectionChangeAction.Add:
                    {
                        var rowTJLX = gridView1.GetRowCellValue<string>(e.ControllerRow, "TJLX");
                        if ((rowTJLX != this.zxlx && this.zxlx=="注射") || (rowTJLX == "注射" && this.zxlx != "注射"))
                        {
                            gridView1.UnselectRow(e.ControllerRow);
                            XtraMessageBox.Show("错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        var id = gridView1.GetRowCellValue<string>(e.ControllerRow, "ID");
                        var barTxt = gridView1.GetRowCellValue<string>(e.ControllerRow, "barTxt");
                        foreach (DataRow dr in dt.Select($"barTxt = '{barTxt}' And ID <> '{id}'"))
                        {
                            var rowHandle = gridView1.LocateByValue("ID", dr.TryGetValue<string>("ID"));
                            if (!gridView1.IsRowSelected(rowHandle))
                            {
                                gridView1.SelectRow(rowHandle);
                            }
                        }
                    }
                    break;
                case CollectionChangeAction.Remove:
                    {
                        var id = gridView1.GetRowCellValue<string>(e.ControllerRow, "ID");
                        var barTxt = gridView1.GetRowCellValue<string>(e.ControllerRow, "barTxt");
                        foreach (DataRow dr in dt.Select($"barTxt = '{barTxt}' And ID <> '{id}'"))
                        {
                            var rowHandle = gridView1.LocateByValue("ID", dr.TryGetValue<string>("ID"));
                            if (gridView1.IsRowSelected(rowHandle))
                            {
                                gridView1.UnselectRow(rowHandle);
                            }
                        }
                    }
                    break;
                case CollectionChangeAction.Refresh://全选反选
                    {
                        if (e.ControllerRow < 0 && gridView1.GetFocusedValue().Equals(true) && gridView1.FocusedColumn.FieldName == "DX$CheckboxSelectorColumn")
                        {
                            if (this.dt.AsEnumerable().Any(r => {
                                var rowTJLX = r.Field<string>("TJLX");
                                return (rowTJLX != this.zxlx && this.zxlx == "注射") || (rowTJLX == "注射" && this.zxlx != "注射");
                            })
                            )
                            {
                                XtraMessageBox.Show("错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                gridView1.ClearSelection();
                            }
                        }
                    }
                    break;
            }
        }

以上就是知道不介绍的《靠SelectionChanged事件撑起来的Gridview CheckBox&MultiSelection 全选、反选\选择事件》,希望对你有所帮助。

声明:若您对本文内容有任何意见,欢迎发送邮件至:admin@zhidaobu.com,本站会尽快回应并做出相应处理。