2014-10-25 4 views
0

У меня есть datagridview со многими столбцами и многими строками данных, и я хочу установить цвет ячеек на красный или зеленый, так что я использовал ниже код
И поскольку мой код иллюстрирует столбец, «экзамен» не читается и не входит в условие if.please, предложите мне, что делать для моей проблемы.Как установить цвет значения ячейки datagridview на основе условия if

dataGridView2_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)//I have used this event 

string unit = Convert.ToString(dataGridView2.Columns["exam"]);//One of my datagridview 
//column name is exam and in that column cells will be unit1 or unit2 or unit3 or unit 4 or quarterly or halfyearly or yearly 
      if (unit == "Unit1" || unit == "Unit2" || unit == "Unit3" || unit == "Unit4") 
       { 
        for (int i = 0; i < dataGridView2.Rows.Count; i++) 
        { 

         for (int j = 7; j < dataGridView2.Rows[i].Cells.Count; j++) 
         { 
          if (Convert.ToInt32(dataGridView2.Rows[i].Cells[j].Value) < 13) 
          { 
           dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Red; 
          } 


          else 
          { 
           dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Green; 
          } 
         } 
        } 

       } 
       else 
       { 
        for (int i = 0; i < dataGridView2.Rows.Count; i++) 
        { 

         for (int j = 7; j < dataGridView2.Rows[i].Cells.Count; j++) 
         { 

          if (Convert.ToInt32(dataGridView2.Rows[i].Cells[j].Value) < 35) 
          dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Red; 
         else 
           dataGridView2.Rows[i].Cells[j].Style.ForeColor = Color.Green; 
         } 
        } 
       } 
+0

http://www.aspsnippets.com/Articles/Dynamically-change-GridView-Row- Background-Color-on-condition-in-ASPNet-using-C-and-VBNet.aspx – Manoj

ответ

0

Это код, что я хотел точно ..
Я надеюсь, что это поможет другим ..

private void dataGridView3_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) 
    { 
     for (int i = 0; i < dataGridView3.Rows.Count; i++) 
     { 
      string unit = Convert.ToString(dataGridView3.Rows[i].Cells[6].Value); 
      if (unit == "Unit1" || unit == "Unit2" || unit == "Unit3" || unit == "Unit4") 
      { 
       for (int k = 7; k < dataGridView3.Rows[i].Cells.Count; k++) 
       { 
        int val = Convert.ToInt32(dataGridView3.Rows[i].Cells[k].Value); 
        if (val <= 12) 
        { 
         dataGridView3.Rows[i].Cells[k].Style.ForeColor = Color.Red; 
        } 

        else 
        { 
         dataGridView3.Rows[i].Cells[k].Style.ForeColor = Color.Green; 
        } 

       } 
      } 

      else 
      { 
       for (int j = 7; j < dataGridView3.Rows[i].Cells.Count; j++) 
       { 
        if (Convert.ToInt32(dataGridView3.Rows[i].Cells[j].Value) < 35) 
        { 
         dataGridView3.Rows[i].Cells[j].Style.ForeColor = Color.Red; 
        } 
        else 
         dataGridView3.Rows[i].Cells[j].Style.ForeColor = Color.Green; 
       } 
      } 
     } 
    } 
0

Вместо использования == использовать обозначение .Equals(), чтобы проверить, являются ли они равны или нет.

if (unit.Equals("Unit 1") ||unit.Equals("Unit1") || unit.Equals("Unit2") || unit.Equals("Unit3") || unit.Equals("Unit4")) 
    { 
     //your logic 
    } 

применить вышеуказанную логику, а также использовать

string data = (string)MyDataGridView[Colnum, Rownum].Value; 

Тогда вы можете просто строки и столбцы петли

Ура! Надеюсь, поможет.

+0

Я не вижу причин, почему это действительно поможет, учитывая код в вопросе. У вас есть повод думать, что проблема '==' здесь? –

+0

«==» сравнивает, если ссылки на объекты одинаковы, а «.Equals()» сравнивается, если содержимое одинаково. –

+0

Мой код только читает еще часть не если часть..пожалуйста, прочитайте мою проблему один раз –

Смежные вопросы