I have a Grid
(not a DataGrid, but a real Grid), with GridLines
set to True
. How can I change the color of the gridlines? Hardcoded in XAML is ok, since it is just for development-reasons.
<Grid ShowGridLines="True" />
Select the worksheets for which you want to change the gridline color. Click File > Excel > Options. In the Advanced category, under Display options for this worksheet, make sure that the Show gridlines check box is selected. In the Gridline color box, click the color you want.
First Method: By typing XAML Code ColumnDefinitions property. By default, GridLines are invisible. For showing the GridLines, set the ShowGridLines property of the Grid to True. GridLines are helpful during debugging for determining which element is in which cell.
A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.
A Grid Panel provides a flexible area which consists of rows and columns. In a Grid, child elements can be arranged in tabular form. Elements can be added to any specific row and column by using Grid.Row and Grid.Column properties. By default, a Grid panel is created with one row and one column.
Sorry, can't be done with ShowGridLines - you need to style the elements contained.
Exhibit A:
MSDN docs say "Only dotted lines are available because this property is intended as a design tool to debug layout problems and is not intended for use in production quality code. If you want lines inside a Grid, style the elements within the Grid to have borders."
Exhibit B - The WPF Source Code:
Notice the Brushes.Blue and Brushes.Yellow hard-coded in this sealed internal class which System.Windows.Controls.Grid uses to draw the lines.
From Grid.cs
/// <summary> /// Helper to render grid lines. /// </summary> internal class GridLinesRenderer : DrawingVisual { /// <summary> /// Static initialization /// </summary> static GridLinesRenderer() { s_oddDashPen = new Pen(Brushes.Blue, c_penWidth); DoubleCollection oddDashArray = new DoubleCollection(); oddDashArray.Add(c_dashLength); oddDashArray.Add(c_dashLength); s_oddDashPen.DashStyle = new DashStyle(oddDashArray, 0); s_oddDashPen.DashCap = PenLineCap.Flat; s_oddDashPen.Freeze(); s_evenDashPen = new Pen(Brushes.Yellow, c_penWidth); DoubleCollection evenDashArray = new DoubleCollection(); evenDashArray.Add(c_dashLength); evenDashArray.Add(c_dashLength); s_evenDashPen.DashStyle = new DashStyle(evenDashArray, c_dashLength); s_evenDashPen.DashCap = PenLineCap.Flat; s_evenDashPen.Freeze(); }
var T = Type.GetType("System.Windows.Controls.Grid+GridLinesRenderer," + " PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); var GLR = Activator.CreateInstance(T); GLR.GetType().GetField("s_oddDashPen", BindingFlags.Static | BindingFlags.NonPublic).SetValue(GLR, new Pen(Brushes.Black, 1.0)); GLR.GetType().GetField("s_evenDashPen", BindingFlags.Static | BindingFlags.NonPublic).SetValue(GLR, new Pen(Brushes.Black, 1.0)); myGrid.ShowGridLines = true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With