在构建DataGridView
. 所以我会使用:
if ( /*I want to change this row */)
{
DataGridViewCellStyle rowStyle; // = Grid.RowHeadersDefaultCellStyle;
rowStyle = Grid.Rows[i].HeaderCell.Style;
rowStyle.BackColor = Color.Wheat;
Grid.Rows[i].HeaderCell.Style = rowStyle;
}
这样你就可以rowStyle
用预定义的样式填充你的样式,然后只更改你想要更改的部分。看看这是否能解决您的问题。
//EDIT 由于您希望保留默认 Windows DataGridView 的其他样式,您还需要设置更多样式的其他参数。看到这个帖子。
或者试试这个。初始化时:
dataGridView1.CellPainting +=
new DataGridViewCellPaintingEventHandler (dataGridView_CellPainting);
然后使用以下内容创建处理程序函数:
void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
DataGridView dv = sender as DataGridView;
DataGridViewCellStyle rowStyle;// = dv.RowHeadersDefaultCellStyle;
if (e.ColumnIndex == -1)
{
e.PaintBackground(e.CellBounds, true);
e.Handled = true;
if (/*I want to change this row */)
{
rowStyle = dv.Rows[e.RowIndex].HeaderCell.Style;
rowStyle.BackColor = Color.Wheat;
dv.Rows[e.RowIndex].HeaderCell.Style = rowStyle;
using (Brush gridBrush = new SolidBrush(Color.Wheat))
{
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Clear cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
//Bottom line drawing
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
// here you force paint of content
e.PaintContent(e.ClipBounds);
e.Handled = true;
}
}
}
}
}
}
此代码基于此帖子。 只有这样,您才需要为鼠标悬停和选定状态创建更多绘制条件。但这应该对你有用。
记得删除:Grid.EnableHeadersVisualStyles = false;
或强制它:Grid.EnableHeadersVisualStyles = true;
。