C # DataGridView Colored text of one row

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

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

I wrote some code to change the text of the string that matches the ID that I pass to the method in Red, but nothing I've 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 the code I've tried, but the corresponding cells never change!

Thanks Neil

+2


source to share


8 answers


Try this format:

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

      



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

+10


source


Use the CellFormatting DataGridView event to make changes to the individual cells that make up the row.

Something like this (be careful 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;
  }
}

      

+5


source


This will work great

private void grid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  DataGridViewRow row = grid1.Rows[e.RowIndex];// get you required index
  // check the cell value under your specific column and then you can toggle your colors
  row.DefaultCellStyle.BackColor = Color.Green;
}

      

+1


source


You need to change individual cells.

This is the VB code pulled from the app where I can do it successfully but not hard to convert it 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

      

0


source


I just checked your code (installation ForeColor

with DefaultCellStyle

) and it works (since .NET 3.5, but I don't think Winforms has evolved since 2.0).

Now I don't know why this doesn't work for you ... maybe you are calling the code before adding lines, or reloading lines after your code is called?

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

0


source


I believe the solution lies in WHEN you set the color, not how you do it. Several different events have been suggested and some of them will actually work. One of the problems with using either cellformatting, databindingcomplete, or even paint events is that they get fired multiple times. From what I've gathered, the problem with the datagridview control is that you can't change the color of any of the cells until AFTER the form is displayed. This way, triggered methods or events that are fired before Shown () are called will not change color. Events that are seen as a solution to a problem usually work, but since they are called many times, it may not be the most efficient response.

Probably the simplest solution to this problem is that your code should fill / color your grids in the Shown () method of your form instead of the constructor. Below is a link to a post in the msdn forums that upset me for a solution, it is marked as an answer about 3/4 way down the page.

Post to MSDN Forums with Solution

0


source


There seems to be a bug with TabControl

that when it contains more than one tab , the ones cellstyles

created with the code only apply to the first tab of the DataGridView , so to solve the problem you can move DataGridView

to the first tab or you can use the TabControl event SelectedIndexChanged

and put your style code in this event.

0


source


row.DefaultCellStyle.ForeColor = Color.Red;

      

This works for .NET 4.0 and upwards at least.

0


source







All Articles