Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Color for separator lines in DataGridView
 

Color for separator lines in DataGridView

Is there easy way ( if any ) to change color for horisontal lines separating rows and vertical lines separating columns from gray to something else. Also any possibility to change shape ( dotted , dashed ) for those lines. My aim to mimick look and feel of HTML table with DataGridView.
MSMit  Tuesday, August 21, 2007 7:19 AM
GridColor will change the color of the grid lines. I can't think of a way to change the pen type. I did however find a CellBorderStyle and DataGridViewAdvancedBorderStyle properties that let you change the styles.
ssta  Tuesday, August 21, 2007 7:30 AM

To have dot style or other style gridlines, you can handle the CellPainting event to custom paint the cells

Code Snippet

dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)

{

e.PaintParts &= ~DataGridViewPaintParts.Border;

}

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.ColumnIndex > -1 && e.RowIndex > -1)

{

e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);

Pen pen = new Pen(this.dataGridView1.GridColor);

pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;

//draw bottom border side

e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

e.CellBounds.Right, e.CellBounds.Bottom - 1);

//draw right border side

e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top,

e.CellBounds.Right - 1, e.CellBounds.Bottom);

e.Handled = true;

}

}

GridColor will change the color of the grid lines. I can't think of a way to change the pen type. I did however find a CellBorderStyle and DataGridViewAdvancedBorderStyle properties that let you change the styles.

ssta  Tuesday, August 21, 2007 7:30 AM

To have dot style or other style gridlines, you can handle the CellPainting event to custom paint the cells

Code Snippet

dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)

{

e.PaintParts &= ~DataGridViewPaintParts.Border;

}

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.ColumnIndex > -1 && e.RowIndex > -1)

{

e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);

Pen pen = new Pen(this.dataGridView1.GridColor);

pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;

//draw bottom border side

e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

e.CellBounds.Right, e.CellBounds.Bottom - 1);

//draw right border side

e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top,

e.CellBounds.Right - 1, e.CellBounds.Bottom);

e.Handled = true;

}

}

You can use google to search for other answers

Custom Search

More Threads

• c# How to use ComboBox independent Value to determine What to Display in DatagridView?
• Warning: "DataMember property 'Users' cannot be found on the DataSource" ?
• Host Controls in Windows Forms DataGridView Cells
• DataGridView binding to complex business objects
• Databind one control to another
• Update/Refresh data in DataGridView from database
• newbie question on combo box lookups
• can't add a table to my dataset
• GridVeiw Delete problem
• Datagridview problem in visual studio 2005