一、常用属性

1)去除 gridView 头上的 "Drag a column header here to group by that column"

    gridView.OptionsView.ShowGroupPanel = false;

2)显示出 gridView 自带的 搜索功能 

    gridView.OptionsFind.AlwaysVisible = true;

3)让各列头禁止移动

    gridView.OptionsCustomization.AllowColumnMoving = false;

4)让各列头禁止排序

    gridView.OptionsCustomization.AllowSort = false;

5)禁止各列头改变列宽

    gridView.OptionsCustomization.AllowColumnResizing = false;

6)设置gridView不可编辑

    gridView.OptionsBehavior.Editable = false;

7)设置GridColumn(列)不可编辑

    gridColumn.OptionsColumn.AllowEdit = false;

8)不显示View的详细信息。(不显示每行前面的”+“)

    gridView.OptionsDetail.EnableMasterViewMode = false;

9)判断gridView是否选中了数据

    int index= this.gridData.gridView.GetFocusedDataSourceRowIndex() ;

    如果index小于0,证明没有选中行,否则就选中了行数据

10)选中某一行

    gridView.FocusedRowHandle =i;

    gridView.SelectRow(i);

11)根据内容自适应列宽

    gridView.BestFitColumns();

12)获取选中行的数据

    DataRow row = gridView.GetRow(i);

    DataRow row =gridView.GetFocusedRow();

13)新增一行

    gridView.AddNewRow()

14)删除选中的行

    gridView.DeleteSelectedRows();

15)设置某一列不可编辑

    GridColumn.OptionsColumn.AllowEdit = false;

16)设置gridControl列宽自使用

    gridView.OptionsView.ColumnAutoWidth = True;//为false宽度不够时出现水平滚动条

17)绑定数据

    //绑定DataSet

    gridControl.DataSource = DataSet;

    gridControl.DataMember = "表名";

    //绑定List集合

    gcGumRecord.DataSource = new BindingList<类名>(集合);

二、常用设置

1)设置成一次选择一行,并且不能被编辑

    this.gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;

    this.gridView.OptionsBehavior.Editable = false;

    this.gridView.OptionsSelection.EnableAppearanceFocusedCell = false;

2)设置gridView为多选模式

    gridView.OptionsSelection.MultiSelect = true;

    gridView.OptionsSelection.CheckBoxSelectorColumnWidth = 30;

    gridView.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;

    效果:

    DevExpress GridControl使用整理

    ps:不设置多选模式时:gridView.OptionsSelection.MultiSelect = false;

3)gridView为多选模式时设置单选(不能同时勾选多个),使用SelectionChanged事件

private void GridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
{
    var a = e.Action;
    if (gridView1.SelectedRowsCount > 1)
    {
        foreach (int rowHandle in gridView1.GetSelectedRows())
        {
            if (rowHandle != e.ControllerRow)
            {
                gridView1.UnselectRow(rowHandle);
            }
        }
    }
}
View Code

相关文章: