Change borders for a specific cell at runtime

3 posts, 0 answers
  1. Lorenzo
    Lorenzo avatar
    11 posts
    Member since:
    Nov 2011

    Posted 01 Mar 2012 Link to this post

    Hi,

    I am trying to change border for a specific cell at runtime. I exactly know what cell I have to update and I don't want to use CellFormatting event handler for performance issues.

    I am using the following code to accomplish this task:

    GridViewCellInfo cellInfo = radGridView.Rows[i].Cells[j];
    cellInfo.Style.DrawFill = true;
    cellInfo.Style.NumberOfColors = 1;
    cellInfo.Style.CustomizeFill = true;
    cellInfo.Style.DrawBorder = true;
    cellInfo.Style.CustomizeBorder = true;
    cellInfo.Style.BorderWidth = 1;
    cellInfo.Style.BorderColor = Color.Red;
    cellInfo.Style.BorderColor2 = Color.Red;
    cellInfo.Style.BorderColor3 = Color.Red;
    cellInfo.Style.BorderColor4 = Color.Red;
    cellInfo.Style.BackColor = Color.Yellow;

    It works fine, but the bottom and right cell borders are not updated. What's wrong? Can you help me to fix that?

    Thanks in advance,
    Lorenzo
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 06 Mar 2012 Link to this post

    Hello Lorenzo,

    Thank you for writing.

    The reason for this behavior is because of the cell spacing and row spacing. By default these are set to -1 so cells and rows overlap each other. To work around it, you can do couple of things. One is to set the RadGridView.TableElement RowSpacing and CellSpacing properties to 0. This will make all cells and rows to be placed exactly next to each other without overlapping and will make your code work. The second solution is to modify the neighboring cells, precisely the cell under and the cell on the right. Here is a method which does this using a row index and a column index:
    private void StyleCell(int rowIndex, int columnIndex)
    {
      GridViewCellInfo cellInfo = this.radGridView1.Rows[rowIndex].Cells[columnIndex];
      cellInfo.Style.DrawFill = true;
      cellInfo.Style.NumberOfColors = 1;
      cellInfo.Style.CustomizeFill = true;
      cellInfo.Style.DrawBorder = true;
      cellInfo.Style.CustomizeBorder = true;
      cellInfo.Style.BorderBoxStyle = BorderBoxStyle.FourBorders;
      cellInfo.Style.BorderWidth = 1;
      cellInfo.Style.BorderLeftColor = Color.Red;
      cellInfo.Style.BorderTopColor = Color.Red;
      cellInfo.Style.BorderRightColor = Color.Red;
      cellInfo.Style.BorderBottomColor = Color.Red;
      cellInfo.Style.BackColor = Color.Yellow;
     
      if (columnIndex < this.radGridView1.TableElement.ViewElement.RowLayout.LastDataColumn.Index)
      {
        GridViewCellInfo rightCellInfo = this.radGridView1.Rows[rowIndex].Cells[columnIndex + 1];
        rightCellInfo.Style.CustomizeBorder = true;
        rightCellInfo.Style.BorderWidth = 1;
        rightCellInfo.Style.DrawBorder = true;
        rightCellInfo.Style.BorderBoxStyle = BorderBoxStyle.FourBorders;
        rightCellInfo.Style.CustomizeBorder = true;
        rightCellInfo.Style.BorderLeftColor = Color.Red;
        rightCellInfo.Style.BorderTopColor = Color.FromArgb(209, 225, 245);
        rightCellInfo.Style.BorderRightColor = Color.FromArgb(209, 225, 245);
      }
     
      if (rowIndex + 1 < this.radGridView1.Rows.Count)
      {
        GridViewCellInfo underCellInfo = this.radGridView1.Rows[rowIndex + 1].Cells[columnIndex];
        underCellInfo.Style.CustomizeBorder = true;
        underCellInfo.Style.BorderWidth = 1;
        underCellInfo.Style.DrawBorder = true;
        underCellInfo.Style.BorderBoxStyle = BorderBoxStyle.FourBorders;
        underCellInfo.Style.CustomizeBorder = true;
        underCellInfo.Style.BorderTopColor = Color.Red;
        underCellInfo.Style.BorderLeftColor = Color.FromArgb(209, 225, 245);
      }
    }

    I hope this will help you. Should you have further questions, I would be glad to assist.

    Regards,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. Lorenzo
    Lorenzo avatar
    11 posts
    Member since:
    Nov 2011

    Posted 06 Mar 2012 Link to this post

    Hi,

    thank you very much Ivan for your help.
    Now I understand and I fixed the problems.

    Regards,
    Lorenzo
Back to Top