XtraGrid使用心得二

时间:2022-04-01 16:37:38
为XtraGrid的GridView加行号。示例代码如下:

//设置行指示器的宽度(行指示器就是最左边那列什么都不显示的列)
//默认情况下,如果要在里面显示行号的话,宽度会不足,数字显示不清晰
//所以要改一下宽度

gv1.IndicatorWidth = 20;

private void gv1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
{
e.Info.DisplayText = (e.RowHandle + 1).ToString().Trim();
}
}

  效果图如下:

  XtraGrid使用心得二

  常用代码:

//添加分组统计字段
gridView1.GroupSummary.Add(SummaryItemType.Count, "Product Name", gridView1.Columns["Product Name"]);
gridView1.GroupSummary.Add(SummaryItemType.Average, "Unit Price", gridView1.Columns["Unit Price"]);

//设置分组统计字段的显示格式
((GridSummaryItem)gridView1.GroupSummary[gridView1.GroupSummary.Count - 1]).DisplayFormat = "AVR={0:c}";

//设置分组字段
gridView1.Columns["Discontinued"].GroupIndex = 0;

//打开所有分组
gridView1.ExpandAllGroups();

//为控件画边框
ControlPaint.DrawBorder3D(e.Graphics,
r,
(e.Info.State == DevExpress.Utils.Drawing.ObjectState.Pressed ? Border3DStyle.SunkenOuter

: Border3DStyle.RaisedInner));


//焦点单元格的列
gridview1.FocusedColumn

//焦点单元格所在行的行号
gridview1.FocusedRowHandle

//焦点单元格的值
gridview1.FocusedValue

//指定单元格显示的字符串值
gridview1.GetRowCellDisplayText(int rowHandler,string feildName)

//指定单元格的值
gridview1.GetRowCellValue(int rowHandler,string feildName)

//设置焦点单元格所在行指定列的值
gridview1.SetFocusedRowCellValue(GridColumn col,object value)

//设置焦点单元格的值
gridview1.SetFocusedValue(object value)

//设置指定单元格的值
//有了这个函数,就可以手工创建行了。

gridview1.SetRowCellValue(int rowHandler,string feildName,object value)
gridview1.SetRowCellValue(int rowHandler,GridColumn col,object value)

//添加新行的方法

gv1.AddNewRow();
foreach (GridColumn col in gv1.Columns)
{
  //用RowCount-1的办法是不行的
  //gv1.SetRowCellValue(gv1.RowCount-1, col, "aaaaa");
  gv1.SetRowCellValue(gv1.FocusedRowHandle, col, "aaaaa");
}

gv1.UpdateCurrentRow();

//根据绑定的数据源自动产生列

gv1.PopulateColumns();

//为列添加下拉列表(加其他类型如日期、UpDown同理)
RepositoryItemLookUpEdit ri = new RepositoryItemLookUpEdit();
//ri.PopupWidth = 200;
ri.DisplayMember = "sc_prna";
ri.ValueMember = "sc_prno";
DataTable dt1 = DbHelperSQL.QueryT("select sc_prno,sc_prna from mespb04h");
ri.DataSource = dt1;
gv1.Columns["sc_prno"].ColumnEdit = ri;