boukaka
asked on
DatagridView Cell border style based on cell contents in C#.
I need to change the cell borders in a datagridview based on it's content. I've figured out how to change the background color but I can't access the border style...
for (int i = 0; i < this.gamesDataGridView.Row s.Count; i++)
{
string cellValue = gamesDataGridView[COL_ID_G AME_TYPE, i].Value.ToString();
if (cellValue == "3")
{
//adjust background color
this.gamesDataGridView[COL _ID_4Q, i].Style.BackColor = Color.LightSlateGray;
// adjust border style to sunken?
}
}
for (int i = 0; i < this.gamesDataGridView.Row
{
string cellValue = gamesDataGridView[COL_ID_G
if (cellValue == "3")
{
//adjust background color
this.gamesDataGridView[COL
// adjust border style to sunken?
}
}
ASKER
I forgot to mention that this is a Windows Form so CSS and Style.Add are not an option....
ASKER
I did find this class online that looks like it would do what I want but I don't know how to apply it to my cell. I'm relatively new to C# ....
class CustomDataGridViewCell : DataGridViewTextBoxCell
{
private DataGridViewAdvancedBorder Style _style;
public CustomDataGridViewCell() : base()
{
_style = new DataGridViewAdvancedBorder Style();
_style.Bottom = DataGridViewAdvancedCellBo rderStyle. Inset;
_style.Top = DataGridViewAdvancedCellBo rderStyle. Inset;
_style.Left = DataGridViewAdvancedCellBo rderStyle. Inset;
_style.Right = DataGridViewAdvancedCellBo rderStyle. Inset;
}
public DataGridViewAdvancedBorder Style AdvancedBorderStyle
{
get { return _style; }
set
{
_style.Bottom = value.Bottom;
_style.Top = value.Top;
_style.Left = value.Left;
_style.Right = value.Right;
}
}
protected override void PaintBorder(Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorder Style advancedBorderStyle)
{
base.PaintBorder(graphics, clipBounds, bounds, cellStyle, _style);
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorder Style advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, _style, paintParts);
}
}
class CustomDataGridViewCell : DataGridViewTextBoxCell
{
private DataGridViewAdvancedBorder
public CustomDataGridViewCell() : base()
{
_style = new DataGridViewAdvancedBorder
_style.Bottom = DataGridViewAdvancedCellBo
_style.Top = DataGridViewAdvancedCellBo
_style.Left = DataGridViewAdvancedCellBo
_style.Right = DataGridViewAdvancedCellBo
}
public DataGridViewAdvancedBorder
{
get { return _style; }
set
{
_style.Bottom = value.Bottom;
_style.Top = value.Top;
_style.Left = value.Left;
_style.Right = value.Right;
}
}
protected override void PaintBorder(Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorder
{
base.PaintBorder(graphics,
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorder
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, _style, paintParts);
}
}
ASKER
Well I managed to get my function to work and now these four cells have borders and look like text boxes.
this.gamesDataGridView[COL _ID_1Q, i] = new CustomDataGridViewCell();
this.gamesDataGridView[COL _ID_2Q, i] = new CustomDataGridViewCell();
this.gamesDataGridView[COL _ID_3Q, i] = new CustomDataGridViewCell();
this.gamesDataGridView[COL _ID_4Q, i] = new CustomDataGridViewCell();
but now I have a slightly different problem, when I click in the cell the borders disappear, then I click the next cell and the borders of the original cell come back but the bottom border is missing. This happens for every cell that I click in. How do I keep the borders from disappearing?
this.gamesDataGridView[COL
this.gamesDataGridView[COL
this.gamesDataGridView[COL
this.gamesDataGridView[COL
but now I have a slightly different problem, when I click in the cell the borders disappear, then I click the next cell and the borders of the original cell come back but the bottom border is missing. This happens for every cell that I click in. How do I keep the borders from disappearing?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I found the solution myself and posted it as well.
Alternately, you can below code.
Open in new window
Change the color code to whatever color you want.
Also add below line to change border style
Open in new window