一、常用属性
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;
效果:
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
4)设置显示gridView新增一行
gridView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
gridView.NewItemRowText = "点击此处增加一行";
效果:
ps:仅设置显示效果,实际能新增行要先给GridControl绑定数据源。
不设置新增一行时: gridView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
21)显示行号
编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"。public static String substring(String str, int toCount,String more)
int re