C# DataGridView

一、鼠标左键取得选中的行this

DataGridViewSelectedRowCollection rows = this.dataGridView1.SelectedRows;orm

二、遍历选中的行io

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

三、给DataGridView第一列绘制行号foreach

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);
}im

四、清空DataGridView全部行co

this.dataGridView.Rows.Clear();data