C# DataGridView

1、鼠标左键取得选中的行

DataGridViewSelectedRowCollection rows = this.dataGridView1.SelectedRows;

2、遍历选中的行

foreach (DataGridViewRow dr in rows)
{
strPersonCode = dr.Cells["colPersonCode"].Value.ToString();
strCardNo = dr.Cells["colCardNo"].Value.ToString();
strNewTime = dr.Cells["colNewTime"].Value.ToString();
}

3、给DataGridView第一列绘制行号

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dataGridView1.RowHeadersWidth - 2,
e.RowBounds.Height);

TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dataGridView1.RowHeadersDefaultCellStyle.Font,
rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}

4、清空DataGridView所有行

this.dataGridView.Rows.Clear();

posted @ 2017-03-20 15:47  你是我的四月天  阅读(426)  评论(0编辑  收藏  举报