1.DataGridView显示行号--RowPostPaint
代码如下:
private void dgvUser_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex < 0) return;
SolidBrush b = new SolidBrush(this.dgvUser.RowHeadersDefaultCellStyle.ForeColor);
e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dgvUser.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
}
2.DataGridView列展示渲染--CellFormatting
private void dgvComplete_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
{
if (dgvComplete.Columns[e.ColumnIndex].Name.Equals("status"))
{
e.Value = switchStatus(e.Value.ToString());
}
}
}
把 状态 这一栏的值从(0,1)展示为(否,是)
3.DataGridView获取某一列的值
int index = dgvComplete.CurrentRow.Index; //取得选中行的索引
string processStatus = dgvComplete.Rows[index].Cells["process"].Value.ToString();
//获取单元格列名为‘process’的值