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.
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.
There is no direct way of doing this. You have to draw your own border in CellPainting
event handler.
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.
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.
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;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.