使用颜色突出显示数据 [英] Highlight data using color

查看:55
本文介绍了使用颜色突出显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用datagridview,我们绑定数据库中的数据。

i希望datagridview索引颜色会根据数字改变。



like value是0然后datagridview索引bg颜色是红色和

值是1然后datagridview索引bg颜色是绿色。

请告诉我吗?



for ex-

表就像那样

名称class1 class2 class3

ram 0 1 1

sita 1 1 0



这里我要求红色bg颜色为0,绿色bg颜色为1.



我尝试了什么:



i想要使用if else条件,但问题是如何检索索引值来自datagridview,我怎样才能以颜色显示。

By using datagridview we bind the data from database.
i want the datagridview index color will change as per number.

like value is 0 then datagridview index bg color is red and
value is 1 then datagridview index bg color is green.
Advise me please?

for ex-
Table is like that
name class1 class2 class3
ram 0 1 1
sita 1 1 0

here i want red bg color for 0 and green bg color for 1.

What I have tried:

i wanted to use if else condition but the problem is that how to retrive the index value from datagridview, and how can i show in color based.

推荐答案

试试这个



try this

private void Form1_Load(object sender, EventArgs e)
      {


  DataTable dt = new DataTable ();
          dt.Columns.Add("name");
          dt.Columns.Add("class1");
          dt.Columns.Add("class2");
          dt.Columns.Add("class3");
          dt.Rows.Add("ram",0,1,1);
          dt.Rows.Add("ram",1,1,0);

          dataGridView1.DataSource = dt;

          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
              foreach (DataGridViewCell cell in row.Cells)
              {
                  string columnName = dataGridView1.Columns[cell.ColumnIndex].Name ;
                  string value = (cell.Value + "").Trim();
                  if ( new string[] { "class1" , "class2","class3"}.Contains(columnName))
                  {
                      if (value == "0")
                          cell.Style.BackColor = System.Drawing.Color.Red;
                      else if  (value == "1")
                          cell.Style.BackColor = System.Drawing.Color.Green;

                  }
              }
          }

      }


您可以单独修改每个单元格的样式。亮点的问题已经解决。

请参阅: DataGridViewCell.Style属性(System.Windows.Forms) [ ^ ]。



细胞索引的问题看起来不仅仅是奇怪的。如果你以某种方式解决了单元格,你使用它的行和列索引。如果你已经有了单元格引用,它会告诉你它的索引:

DataGridViewCell.RowIndex属性(System.Windows.Forms) [ ^ ],

DataGridViewCell.ColumnIndex Property(System.Windows.Forms) [ ^ ]。



换句话说,没有这样的问题。具有这样的问题几乎等同于根本不使用该网格视图控件。只需阅读原始MSDN文档: DataGridView类(系统。 Windows.Forms) [ ^ ]。



-SA
You can modify the style of each individual cell, separately. The problem with highlight is solved.
Please see: DataGridViewCell.Style Property (System.Windows.Forms)[^].

The problem with the cell index looks more than weird. If you somehow addressed the cell, you used it's row and column indices. If you have the cell reference already, it tells you its indices:
DataGridViewCell.RowIndex Property (System.Windows.Forms)[^],
DataGridViewCell.ColumnIndex Property (System.Windows.Forms)[^].

In other words, there is no such problem. Having such "problem" is nearly equivalent to not using this grid view control at all. Just read original MSDN documentation: DataGridView Class (System.Windows.Forms)[^].

—SA


这篇关于使用颜色突出显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆