刚接触DevExpress第三方控件,把GridControl的常见用法整理一下,以供参考:
说明:
gcTest GridControl
gvText GridView
//隐藏最上面的GroupPanel 即去掉"Drag a Column Header Here To Group by that Column"
gvText.OptionsView.ShowGroupPanel = false;
//修改最上面的GroupPanel
gvText.GroupPanelText = "修改后的内容";
//单元格不可编辑
gvText.OptionsBehavior.Editable = false;
//某列标题居中 0是列索引
gvText.Columns[0].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
//某列内容居中 0是列索引
gvText.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
//冻结列
gvText.Columns[0].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
//自动改变行高适应内容
gvText.OptionsView.RowAutoHeight = true;
//显示自动筛选行
gvText.OptionsView.ShowAutoFilterRow = false;
//不显示字表信息
gvText.OptionsView.ShowDetailButtons = false;
//显示水平滚动条,自动列宽
gvText.OptionsView.ColumnAutoWidth=false;
//自动调整列宽
gvText.BestFitColumns();
//禁用GridControl中单击列弹出右键菜单
gvText.OptionsMenu.EnableColumnMenu = false;
//禁用GridControl中列头的过滤器
gvText.OptionsCustomization.AllowFilter = false;
//禁止各列头移动
gvText.OptionsCustomization.AllowColumnMoving = false;
//禁止各列头排序
gvText.OptionsCustomization.AllowSort = false;
//禁止各列头改变列宽
gvText.OptionsCustomization.AllowColumnResizing = false;
//根据绑定的数据源自动产生列,有时可以解决GridControl记录能获取而没有显示出来的问题
gvText.PopulateColumns();
//奇偶行变色
gvText.OptionsView.EnableAppearanceEvenRow = true;
gvText.OptionsView.EnableAppearanceOddRow = true;
gvText.Appearance.EvenRow.BackColor = Color.Gray;
gvText.Appearance.OddRow.BackColor = Color.GreenYellow;
//特殊列设置 日期
string strDate="yyyy-MM-dd HH:mm";
RepositoryItemDateEdit ride = new RepositoryItemDateEdit();
ride.DisplayFormat.FormatString = strDate;
ride.EditFormat.FormatString = strDate;
ride.EditMask = strDate;
gvText.Columns["日期"].ColumnEdit = ride;
//列格式设置 decimal 价格
public const string MoneyFormatStr = "##,###,###,###,##0.00";
RepositoryItemCalcEdit rice = new RepositoryItemCalcEdit();
rice.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
rice.DisplayFormat.FormatString = MoneyFormatStr;
rice.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
rice.EditFormat.FormatString = MoneyFormatStr;
rice.EditMask = MoneyFormatStr;
gv.Columns["价格"].ColumnEdit = rice;
//给单元格赋值
gvText.SetRowCellValue(3, gvText.Columns["列名或列索引"],"要赋的值");
//添加行
gvText.AddNewRow();
//添加列
DevExpress.XtraGrid.Columns.GridColumn col = new DevExpress.XtraGrid.Columns.GridColumn();
col.Caption = "列标题";
col.FieldName = "列字段值";
col.Visible = true;
col.VisibleIndex = gvText.Columns.Count;
gvText.Columns.Add(col);
/// <summary>
/// 获取选定行指定列单元格的值
/// </summary>
/// <param name="str">指定列的列名</param>
/// <returns>单元格的值</returns>
public string GetCellValue(string str) {
int[] pRows = this.gvText.GetSelectedRows();
if (pRows.GetLength(0) > 0){
return gvText.GetRowCellValue(pRows[0], str).ToString();
}
else {
return null;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。