DataGridView runtime row formatting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sklett

    DataGridView runtime row formatting

    I need to format groups of rows separate from other rows depending on a cell
    value. I'm sure others have done this as well.

    For example, let's say I have 6 rows with a property called 'duration' with
    the values:
    2
    1
    5
    5
    5
    7

    I want to change the background color of the rows that have the value 5 to a
    different color.

    I've checked out a couple of the grid's events, but I'm not seeing anything
    that sounds correct for this task. Something like "BindingRow ".

    I suupose that I would need to catch an event for each row's painting or
    binding and then get the datasource for the row and then get the property
    I'm interested in checking, but I'm not clear how to do this.

    Have any of you done this?

    I tried this code and ended up with a really funny result:
    <code>
    int sectionColorInd ex = 0;
    Color[] sectionColors = new Color[]
    {
    Color.Gainsboro ,
    Color.LightSalm on,
    Color.LightSeaG reen,
    Color.LightSkyB lue,
    Color.LightCora l
    };
    private void dataGridView1_C ellFormatting(o bject sender,
    DataGridViewCel lFormattingEven tArgs e)
    {
    if (this.dataGridV iew1.Columns[e.ColumnIndex].Name ==
    "DataGridViewCo mboBoxColumn_Op Code")
    {
    if (e.Value != null)
    {
    dataGridView1.R ows[e.RowIndex].DefaultCellSty le.BackColor =
    sectionColors[sectionColorInd ex];
    if ((string)e.Valu e == "DJMPNZSKIP ")
    {
    sectionColorInd ex++;
    if (sectionColorIn dex 4)
    {
    sectionColorInd ex = 0;
    }
    Debug.Assert(se ctionColorIndex < sectionColors.L ength,
    "Not enough colors to format grid");
    }
    }
    }
    }
    </code>
    it seems that it's constantly calling format on the cells which results in
    an animated DateGrid of my 5 colors. It's funny, but frustrating. I now
    know I'm doing this wrong, please help before I vomoit from the colors!!
    :0)


Working...