0
votes

So I am trying to change the color of a DataGridView row depending on the value of a cell. Basically, I am dropping real files into the DataGridView and it will display the information of that file.

this is what I currently have:

double size;

size = Math.Round(MeuFicheiro.Length / 1024d, 4);

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (size >= 0 && size <= 4999)
    {
    row.DefaultCellStyle.BackColor = Color.Red;
    }
    if (size >= 5000)
    {
    row.DefaultCellStyle.BackColor = Color.Green;
    }
    else if (size >= 15000)
    {
    row.DefaultCellStyle.BackColor = Color.Orange;
    }
}

But this is not working because it does not work because when I insert a new fill it will change the color of all the rows instead of the row I've added.

After making some changes I've got this on the method I've created to display information into the DataGridView:

private void MostrarDataGridView()
{
    try
    {
    con = new SqlConnection(cs.DBConn);
    con.Open();

    cmd = new SqlCommand("SELECT * FROM InfoFile", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "InfoFiel");

    dataGridView1.DataSource = ds.Tables["InfoFile"].DefaultView;

    DataTable dt = ds.Tables["InfoFile"];
    System.Data.DataRowView dgrv = (System.Data.DataRowView)
    string fnome = dgrv.Row.ItemArray[0].ToString();

    con.Close();
    }
}

Do you have any help to give? I just what to change the added row color depending on the value of size.

1
Use the CellFormatting event.LarsTech
Possible but not duplicated @ShengJiang蒋晟LeugimSnitram
The line System.Data.DataRowView dgrv = (System.Data.DataRowView) looks like its missing something...JohnG
Because I don't know what should I put there @JohnGLeugimSnitram

1 Answers

0
votes

To start… Your first sentence and last sentence are contradictory. First you say…

So I am trying to change the color of a DataGridView row depending on the value of a cell.

THEN the last sentence:

I just what to change the added row color depending on the value of size.

Those are two different things, so it is unclear what exactly you want to do.

If you are simply comparing size, then ALL the rows will be same color depending on the value of size. In your code attempt, if size is greater than 15000 then all rows will be orange. But following your code, each row will first get set to red if tamanho is less than 5000, then set again to orange. This logic does not look right.

Example: Setting size = 5001 and tamanho to 4999.

if (size >= 0 && tamanho <= 4999)

In the above if statement it will change the color to red. Then, the next if statement:

if (size >= 5000)

is executed because there is no “else” clause in the previous if statement. This if statement will obligingly change the color to green because size = 5001. So your last setting of the row to red is unnecessary and confusing.

As long as size is less than 5000, all rows will be red if tamanho is less than 5000 and white if tamanho is greater than 4999. If size is greater than 4999 but less than 15000 then all rows will be green no matter what tamanho is. If size is greater than 14999 all rows will be orange no matter what tamanho is.

Since the value of size (and tamanho) never changes while looping thru the rows, your logic will simply change ALL the rows to the color that matches with the unchanging variable size. This will set ALL the rows to the same color depending on the value of size.

So in reference to you first question: change the color of a DataGridView row depending on the value of a cell. In your code you are not grabbing any value from any cells in the DataGridView to compare to anything. If you want to change a rows color when a particular cells value is greater than a set value, then you need to grab that cells value. Below is one example of how to grab the value from a cell in the DataGridView and check its value.

Using your foreach (DataGridViewRow row in dataGridView1.Rows) loop, you can access the FIRST cell/(column) of the current row with the following code.

String stringValue = row.Cells[0].Value.ToString();

If your DataGridView is bound to a DataSet Table then you may/will have to use something different depending on your table. An example is below to get the string from dgrv.

System.Data.DataRowView dgrv = (System.Data.DataRowView)DataTable.CurrentRow.DataBoundItem;

The code below will change the row color for that row if the current row’s ‘Cell[colIndex]’ is greater than ‘valueToCompare’. In this case let’s say the cell value is a string but contains an integer value. So to get this integer value and compare it to valueToCompare we need to parse the string to an integer then compare the values, then change the row color depending on if the cell value is greater than valueToCompare. GetIntValue(string) returns 0 if the string (cell value) is not a valid integer.

private void SetRowColorBasedOnColValue(int valueToCompare, int colIndex, Color color)
{
  foreach (DataGridViewRow row in dataGridView1.Rows)
  {
    if (row.Cells[colIndex] != null && row.Cells[colIndex].Value != null)
    {
      int cellValue = GetIntValue(row.Cells[colIndex].Value.ToString());
      if (cellValue >= valueToCompare)
      {
        row.DefaultCellStyle.BackColor = color;
      }
      else
      {
        row.DefaultCellStyle.BackColor = Color.White;
      }
    }
    else
    {
      // null cell
      row.DefaultCellStyle.BackColor = Color.White;
    }
  }
}

private int GetIntValue(string inString)
{
  int returnValue = 0;
  if (int.TryParse(inString, out returnValue))
    return returnValue;
  return returnValue;
}

Below I used the above method... I set the row color depending on the value of the cell at colIndex. If the cell’s value is greater than valueToCompare then the row color gets changed to color.

// SetRowColorBasedOnColValue(valueToCompare, colIndex, color);
SetRowColorBasedOnColValue(15000, 0, Color.Orange);
SetRowColorBasedOnColValue(5000, 0, Color.Green);
SetRowColorBasedOnColValue(0, 2, Color.Red);

This is not the best approach though as an anonymous function/delegate would be one of many better approaches to this problem as this method only compares if the cell value is “greater than” the supplied value, this severely limits the functionality of the method.

Hope this helps.