序号自动生成
//序号自动生成
/// <summary>
/// 说明:序号自动生成。
/// </summary>
private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
{
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}
//修改列宽
gridView1.IndicatorWidth = 30; //如果未实现需要把列宽设置到load中
}
给表头添加序号
1、gridview
if ( e.Info.Kind == DevExpress.Utils.Drawing.IndicatorKind.Header )
{
e.Appearance.DrawBackground( e.Cache ,e.Bounds );
e.Appearance.DrawString( e.Cache ,"序号" ,e.Bounds );
e.Handled = true;
}
2、bandgridview
if (e.Info.Kind == DevExpress.Utils.Drawing.IndicatorKind.Band)
{
e.Appearance.DrawBackground(e.Cache, e.Bounds);
e.Appearance.DrawString(e.Cache, "序号", e.Bounds);
e.Handled = true;
}
在dt表第一列添加序号
DataTable dt = new DataTable();
dt = ds.Tables[0];
#region 添加序号列
//添加序列号
int rowLength = dt.Rows.Count;
int colLength = dt.Columns.Count;
DataRow[] newRows = new DataRow[rowLength];
DataTable dtNew = new DataTable();
dtNew.Columns.Add("序号");
for (int i = 0; i < colLength; i++)
{
dtNew.Columns.Add(dt.Columns[i].ColumnName);
//复制dt中的数据
for (int j = 0; j < rowLength; j++)
{
if (newRows[j] == null)
newRows[j] = dtNew.NewRow();
//将其他数据填充到第二列之后,因为第一列为新增的序号列
newRows[j][i + 1] = dt.Rows[j][i];
}
}
foreach (DataRow row in newRows)
{
dtNew.Rows.Add(row);
}
//对序号列填充,从1递增
for (int i = 0; i < dt.Rows.Count; i++)
{
dtNew.Rows[i]["序号"] = i + 1;
}
#endregion