Solved

DatagridView Cell border style based on cell contents in C#.

Posted on 2012-04-11
6
4,246 Views
Last Modified: 2012-06-21
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.Rows.Count; i++)
                {
                    string cellValue = gamesDataGridView[COL_ID_GAME_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?

                    }
                }
0
Comment
Question by:boukaka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 2

Expert Comment

by:yogsoft
ID: 37836088
You can use cell's CssClass property and set background-color inside css class.

Alternately, you can below code.

this.gamesDataGridView[COL_ID_4Q, i].Style.Add("background-color", "#FFF000");

Open in new window


Change the color code to whatever color you want.

Also add below line to change border style

this.gamesDataGridView[COL_ID_4Q, i].Style.Add("border", <border setting value>);

Open in new window

0
 

Author Comment

by:boukaka
ID: 37837463
I forgot to mention that this is a Windows Form so CSS and Style.Add are not an option....
0
 

Author Comment

by:boukaka
ID: 37837568
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 DataGridViewAdvancedBorderStyle _style;

        public CustomDataGridViewCell() : base()
        {
            _style = new DataGridViewAdvancedBorderStyle();
            _style.Bottom = DataGridViewAdvancedCellBorderStyle.Inset;
            _style.Top = DataGridViewAdvancedCellBorderStyle.Inset;
            _style.Left = DataGridViewAdvancedCellBorderStyle.Inset;
            _style.Right = DataGridViewAdvancedCellBorderStyle.Inset;
        }

        public DataGridViewAdvancedBorderStyle 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, DataGridViewAdvancedBorderStyle 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, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, _style, paintParts);
        }

    }
0
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 

Author Comment

by:boukaka
ID: 37838881
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?
0
 

Accepted Solution

by:
boukaka earned 0 total points
ID: 37839868
I figured it out but the solution is a bit odd. I changed the border style on the bottom and right to Outset rather than Inset and now everything looks fine.
0
 

Author Closing Comment

by:boukaka
ID: 37854971
I found the solution myself and posted it as well.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question