views:

1546

answers:

4

I have a DataGridView that has a class as its DataSource.

The class contains an ID string, 2x DateTimes and a Boolean.

I have written some code to change the text of a row that matches an ID I pass to method to Red, but nothing I have tried works.

This is what I have so far:

public void ShowInstanceAsTerminated(String id)
{
    foreach (DataGridViewRow dgvRow in dgvRIM.Rows)
    {
     if (dgvRow.Cells[0].Value.ToString() == id)
     {
      dgvRow.DefaultCellStyle.ForeColor = Color.Red;
     }
    }
}

This is one of many variations of code I have tried, but the cells in question never change!!

Thanks Neil

A: 

You need to change the individual cells.

This is VB code lifted from an app where I am able to successfully do this, but it shouldn't be to hard to convert this to c#.

For i As Integer = 0 To ds.Tables("AddressChanges").Rows.Count - 1
                If ds.Tables("AddressChanges").Rows(i)("iSeriesAddress").ToString <> ds.Tables("AddressChanges").Rows(i)("CSIAddress").ToString() Then
                    Me.dgAddressDiscrepancies.Rows(i).Cells("iSeriesAddress").Style.BackColor = Color.Yellow
                    Me.dgAddressDiscrepancies.Rows(i).Cells("CSIAddress").Style.BackColor = Color.Yellow
                End If
Next
David Stratton
He want's to change the Row's ForeColor, this can be done without having to modify each cell's ForeColor/BackColor.
hjb417
In theory you should be right, but in practice, I've found that it doesn't work. Unless something has changed, that is.. My code was .Net 2.0, so it's possible the DataGridView has been updated, but as he said in his question, if you try to apply it to a row, it throws no error, but does not change the color, either. At any rate, my answer is changing the backcolor, not the forecolor but the concept is the same. @Eric J answered with the same idea but in a better, clearer form.
David Stratton
@David Stratton: I've just tested it and it does work (see my answer). For what reason it didn't work in your case? Why would setting the ForeColor for the individual cells work better??
Meta-Knight
+3  A: 

Try this format:

dgvRIM.Rows[myRow].Cells[0].Style.ForeColor = Color.Red;

If you want to set the entire row, loop over all cells.

Eric J.
+1 - this was the same concept I was trying to get across, but you phrased it better.
David Stratton
This didn't work for me.During a debug session, I can see the style applied to the cell, but it never actually changes. Next time around it still isn't set.I wonder if I have to set that row to be the row after changing it. Something like:dgvRIM.Row[myrow] = newRow;Hmmmm.... any thoughts whilst I give it a go?
neildeadman
How about adding a line of code after changing the color to force the datagridview to update? The following code should do that: dvgRIM.Invalidate();
David Stratton
My other idea threw an exception.dvgRIM.Invalidate(); doidn't work either.I don't see what I am doing wrong really.. its a curious one!I have added a CellStyle_Changed and that hasn't yet been hit!!
neildeadman
If you can create a mini example of the problem small enough to post here (or if you can post a link), I'm happy to have a look at the whole thing.
Eric J.
I created a small forms app to test my code... and it worked fine.So I think I must be re-binding the datasource somewhere that resets any changes...
neildeadman
Sorted it!I was indeed setting the DataSource in a second place.. Doh!!All good and I am using the code as Eric J. suggested.Thanks
neildeadman
+3  A: 

Use the DataGridView's CellFormatting event to make changes to individual cells that compose a row.

Something like this (beware, not tested):

private void dgvRIM_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
  if (dgvRIM.Rows[e.RowIndex].Cells[0].Value.ToString() == id) {
    e.CellStyle.ForeColor = Color.Red;
  }
}
Jay Riggs
+1. You can use e.Value.
Vivek
+1. Vivek, he cannot use e.Value, he wants to check the value of the row's first cell which is not necessarily the current cell.
Meta-Knight
A: 

I just tested your code (setting the ForeColor with DefaultCellStyle) and it DOES work (with .NET 3.5, but I don't think Winforms has evolved since 2.0).

Now I don't know why it doesn't work for you... maybe you call the code before rows are added, or you reload the rows after your code is called??

Either way, you're probably better off with Jay Riggs's solution, as it will work even if you add new rows afterwards.

Meta-Knight