C# 如何在DataGridView中设置特定标题单元格的边框颜色
有谁能帮我解决如何在C# 如何在DataGridView中设置特定标题单元格的边框颜色,c#,winforms,datagridview,C#,Winforms,Datagridview,有谁能帮我解决如何在Cwinform中的DataGridView中设置特定标题单元格的边框颜色的问题吗 我在winform中有一个DataGridView,我的要求是在单击标题单元格时设置标题单元格的边框颜色。没有直接的方法。您必须在事件处理程序中绘制自己的边框 有一个类级变量来存储单击的列标题索引 int myClickedColumnHeaderIndex = -1; 订阅以下活动 dataGridView1.CellPainting += dataGridView1_CellPainti
C
winform中的DataGridView
中设置特定标题单元格的边框颜色的问题吗
我在winform中有一个
DataGridView
,我的要求是在单击标题单元格时设置标题单元格的边框颜色。没有直接的方法。您必须在事件处理程序中绘制自己的边框
有一个类级变量来存储单击的列标题索引
int myClickedColumnHeaderIndex = -1;
订阅以下活动
dataGridView1.CellPainting += dataGridView1_CellPainting;
dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
在columnheadermouse中,单击handler,使用class-level变量存储列索引
void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Clicks == 1)
{
dataGridView1.InvalidateCell(myClickedColumnHeaderIndex, -1); // this to trigger paint of the old cell inorder to remove the border drawn earlier.
myClickedColumnHeaderIndex = e.ColumnIndex;
}
}
在CellPaint
事件处理程序中,使用所需颜色绘制边框
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex >= 0 && e.ColumnIndex == myClickedColumnHeaderIndex)
{
e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
using (Pen customPen = new Pen(Color.Blue, 2))
{
Rectangle rect = e.CellBounds;
rect.Width -= 2;
rect.Height -= 2;
e.Graphics.DrawRectangle(customPen, rect);
}
e.Handled = true;
}
}
此代码为每个偶数列的标题单元格和数据单元格绘制垂直边框
private void DgvCalendar_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int columnIndex = 0;
if (e.RowIndex >= 0 && e.ColumnIndex >= columnIndex)
{
if (e.ColumnIndex % 2 == 0)
{
var brush = new SolidBrush(dgvCalendar.ColumnHeadersDefaultCellStyle.BackColor);
e.Graphics.FillRectangle(brush, e.CellBounds);
brush.Dispose();
e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground);
ControlPaint.DrawBorder(e.Graphics, e.CellBounds,
System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid,
System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid,
System.Drawing.Color.CornflowerBlue, 1, ButtonBorderStyle.Solid,
System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid);
e.Handled = true;
}
}
if (e.RowIndex == -1 && e.ColumnIndex >= columnIndex)
{
if (e.ColumnIndex % 2 == 0)
{
e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
ControlPaint.DrawBorder(e.Graphics, e.CellBounds,
System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid,
System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid,
System.Drawing.Color.CornflowerBlue, 1, ButtonBorderStyle.Solid,
System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid);
e.Handled = true;
e.Handled = true;
}
}
}
谢谢你给我这个有用的主意。可以快速设置上、左、右、下边框。@LhoreBansal-是的,可以使用e.Graphics.DrawLine
和e.cellbunds
中的点分别绘制上、左、右、下边框。如果它有效,你能接受它作为答案吗。