【问题标题】:how to change row colour of datagridview where cell value is some value in c#如何更改datagridview的行颜色,其中单元格值是c#中的某个值
【发布时间】:2014-10-21 06:33:57
【问题描述】:

我正在使用 win-apps,使用 c# 进行编码。
我有一个从 microsoft sql server 数据库加载数据的 datagridview。 我的需要是我想设置datagridview单元格值小于35的行颜色..
我的想法如下。

//this is just my Idea,Not correct code,so please add code  
 private void colorchange()
    {
        if (dataGridView4.cellvaue <= 35)
        {
            dataGridView4.row_fore_colour = red;
        }
    }  

datagridview 单元格值是文本格式。所以请不要忘记将文本转换为整数值,然后给我解决我的问题(我不知道 datagridview 单元格值的转换)

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:
    void dataGridView4_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dataGridView4.Rows[e.RowIndex].Cells["cellToCheck"].Value != null)
            {
                if ((int)this.dataGridView4.Rows[e.RowIndex].Cells["cellToCheck"].Value <=35)
                {
                    this.dataGridView4.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                }
    
            }
        }
    

    我在我想要的代码项目中找到了这个..

    【讨论】:

      【解决方案2】:

      认为这可能是这样做的方法:

          for (int i = 0; i < dataGridView4.Rows.Count; i++)
                  {
                      for (int o = 0; o < dataGridView4.Rows[i].Cells.Count;o++ )
                      {  
                           int num1;
                           if(dataGridView4.Rows[i].Cells[o].Value != null)
                           {
                             string text1 = dataGridView4.Rows[i].Cells[o].Value.toString;
                             bool res = int.TryParse(text1, out num1);
                             if (res == true)
                              {
                                if(Convert.ToInt32(dataGridView4.Rows[i].Cells[o].Value) < 35)
                               {
                                dataGridView4.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Red; 
                               }
                              }
                            }
      
                      }
                  }
      

      【讨论】:

      • 抛出错误输入字符串的格式不正确。
      • 那么你应该在转换它之前检查它是否是一个数字,谷歌这个:TryParse 并将它的工作到代码中。希望对你有帮助
      • 对不起,我没有尝试使用 tryParse.. 请您使用 tryParse 进行更新
      • 没问题,如果您认为它比将其作为接受的答案或投票有用的话。祝你未来好运
      【解决方案3】:

      试试这个可能有用

      void dataGridView4_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
          {
      for (int i = 0; i < dataGridView4.Rows.Count; i++)
                  {
              if (dataGridView4.Rows[i].Cells["cellname"].Value != null)
              {
                  if ((int)dataGridView4.Rows[i].Cells["cellname"].Value <=35)
                  {
                      dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                  }
              }
          }
      }
      

      这将只检查一列“cellname”,如果你想检查所有列然后添加一个额外的for循环..

      【讨论】:

        【解决方案4】:

        您可以在您的情况下使用手机号码。我使用单元格编号解决了我的问题,然后使用 if 条件获取单元格值。您可以使用此代码。

        void dataGridView4_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
        for (int i = 0; i < dataGridView4.Rows.Count; i++)
            {
                if (e.ColumnIndex == 9)
                {
                    if (dataGridView2.CurrentCell != null && dataGridView2.CurrentCell.Value != null && dataGridView2.CurrentCell.Value <=35)
                    {
                    dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    }
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-06
          • 2017-02-05
          • 1970-01-01
          • 2015-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-12
          相关资源
          最近更新 更多