[英]How to set the border color of a particular header cell in DataGridView
Can any one help me to resolve the issue of how to set the border color of a particular header cell in a DataGridView
in a C#
winform. 任何人都可以帮我解决如何在C#
winform中的DataGridView
中设置特定标题单元格的边框颜色的问题。
I have a DataGridView
in C#
winform and my requirement is that I want to set the border color of header cell when we click on header cell. 我在C#
winform中有一个DataGridView
,我的要求是当我们点击标题单元格时我想设置标题单元格的边框颜色。
There is no direct way of doing this. 没有直接的方法可以做到这一点。 You have to draw your own border in CellPainting
event handler. 您必须在CellPainting
事件处理程序中绘制自己的边框。
Have a class level variable to store the clicked column header index. 有一个类级变量来存储单击的列标题索引。
int myClickedColumnHeaderIndex = -1;
Subscribe to below events. 订阅以下活动。
dataGridView1.CellPainting += dataGridView1_CellPainting;
dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
In ColumnHeaderMouseClick
handler store the column index using the class level variable. 在ColumnHeaderMouseClick
处理程序中,使用类级别变量存储列索引。
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;
}
}
In the CellPainting
event handler, draw the border using the required color. 在CellPainting
事件处理程序中,使用所需颜色绘制边框。
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;
}
}
This code draws vertical borde for header cells and data cells for each even column. 此代码为每个偶数列的标题单元格和数据单元格绘制垂直边框。
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.