views:

46

answers:

3

I have a DataGridView bound to a DataTable in a DataSet.

I set all columns of the DataTable to System.Double.

I want the last row of the DataGridView to display "PASS" or "FAIL" depending on some condition of the values in that column.

How do I do this?

Ideas:
lastCell = IIF(condition, Double.PositiveInfinity, Double.NegativeInfinity)
Then, apply some conditional formatting (Inf -> PASS, -Inf -> FAIL) to the final row of the DataGridView.

A: 

Why not add an extra column to the table of type boolean? Booleans are more appropriate here, since you have one of two states, it either passes (true), or doesn't pass (fail).

You can add the extra DataColumn and populate the values manually, or you can use a computed DataColumn and then bind the list to that having a "PASS" column on your DataGridView.

casperOne
The PASS/FAIL applies to the values of that column. It would make more sense to have it be the last row. Also, transposing the grid (to make the status collection a column instead of row) would not make sense in my situation.
Steven
A: 

Build your condition in such a way that it evaluates to >= 0 for "PASS" or < 0 for "FAIL", and apply it before binding to the grid (perhaps in the query itself). Then use this format string on the DefaultCellStyle.Format property for the column in the grid:

{0:"PASS";"FAIL";}

Joel Coehoorn
I only want the formatting applied to the final row.
Steven
+2  A: 

You could probably create and use a custom NumberFormatInfo, setting PositiveInfinitySymbol and NegativeInfinitySymbol to the strings you want.

But I'd say it would be better to do something like this in the DataGridView.CellFormatting event handler - which makes it much clearer what you're doing and why, and won't have undesirable side effects e.g. if another cell happens to have an infinite value because of a division by zero.

Something like the following air code:

void dataGridView1_CellFormatting(object sender, 
    DataGridViewCellFormattingEventArgs e)
{
    if ((e.ColumnIndex == whatever) && (c.RowIndex == whatever))
    {
        // ... comment explaining what you're doing
        if (condition)
        {
            e.Value = "PASS";
        }
        else
        {
           e.Value = "FAIL";
        }
        e.FormattingApplied = true;
    }
}

Apologies for the C# code given you've tagged the question VB.NET, but you'll find a VB.NET sample in the MDSN documentation for the CellFormatting event.

Joe