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 - 4,
e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dataGridView1.RowHeadersDefaultCellStyle.Font,
rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
} private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{ if (this.dataGridView1.Rows.Count != 0)
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i += 2)
{
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Cyan;
}
}
}
以上是我想加入的重绘函数.但我每次用新的DGV,都要加入这两个函数.有没有什么方法,可以使得自己以后直接使用DGV就会自动重绘这两个函数的方法.也就是说,能不能建成一个类,这个类直接包含了以上两个函数?俺不会,我想肯定是可以的,请详细说一下步骤.谢谢.
protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
{
base.OnDataBindingComplete(e);
if (this.Rows.Count != 0)
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i += 2)
{
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Cyan;
}
} }