在Datagridview中繪製行號及進度條
如下圖所示,在每行前面有改行的行號datagridview的第四列為繪製的矩形,可以直觀反應出百分比。
在dataGridView1_RowPostPaint事件中新增如下程式碼
///繪製行號
Rectangle rect = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.dataGridView1.RowHeadersWidth,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.dataGridView1.RowHeadersDefaultCellStyle.Font,
rect, this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
///繪製矩形部分
double value = 0;
double.TryParse(dataGridView1.Rows[e.RowIndex].Cells[colName].Value.ToString(), out value);
int height = dataGridView1.GetCellDisplayRectangle(3, e.RowIndex, false).Height;
Rectangle recColor = new Rectangle(e.RowBounds.Location.X + dataGridView1.GetCellDisplayRectangle(3, e.RowIndex, false).X,
e.RowBounds.Location.Y, Convert.ToInt32(value * dataGridView1.GetCellDisplayRectangle(3, e.RowIndex, false).Width / 100),
height - 2);
e.Graphics.FillRectangle(new SolidBrush(Color.LightPink), recColor);