如何根据条件在C#中设置数据表的颜色 [英] How to set color for datatable in C# based on condition

查看:143
本文介绍了如何根据条件在C#中设置数据表的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有100条记录的数据表,我需要为所有数据表行设置总共6种颜色。需要为我拥有的6种颜色设置前6行。接下来要追加6行。同样我需要设置颜色



我尝试过:



 for(int i = 0; i< GetDetails.Rows.Count; i ++)
{
for(int j = 0; j< dt.Rows.Count; j ++ )
{
if(Convert.ToDateTime(dt.Rows [j] [CREATED_DATE])。ToString(dd / MM / yyyy)== Convert.ToDateTime(GetDetails.Rows [i ] [CREATED_DATE])。ToString(dd / MM / yyyy))
{
if(j%2 == 0)
{
GetDetails.Rows [ i] [color] =#FB707D;
}
否则if(j%3 == 0)
{
GetDetails.Rows [i] [color] =#39CECD;
}
其他
{
GetDetails.Rows [i] [color] =#8677EF;
}
}
}

}

解决方案

< blockquote>你不能在DataTable上设置颜色:它不是一个表示控件,它处理实际的显示 - 它只是数据的存储机制,它不知道它提供的数据将是什么环境用过的。您将数据的颜色设置为显示控件的一部分,具体操作方式取决于环境和控件本身。



例如,这个显示如何为WinForms DataGridView执行此操作:着色DataGridView单元格根据他们的WinForms中的内容 [ ^ ]

Im having datatable with 100 records, i need to set 6 colours in total for all the datatable rows. First 6 rows need to be set for the 6 colours which im having. Next 6 rows to be appended. Similarly i need to set color

What I have tried:

for (int i = 0; i < GetDetails.Rows.Count; i++)
              {
                  for (int j = 0; j < dt.Rows.Count; j++)
                  {
                      if (Convert.ToDateTime(dt.Rows[j]["CREATED_DATE"]).ToString("dd/MM/yyyy") == Convert.ToDateTime(GetDetails.Rows[i]["CREATED_DATE"]).ToString("dd/MM/yyyy"))
                      {
                          if (j % 2 == 0)
                          {
                              GetDetails.Rows[i]["color"] = "#FB707D";
                          }
                          else if (j % 3 == 0)
                          {
                              GetDetails.Rows[i]["color"] = "#39CECD";
                          }
                          else
                          {
                              GetDetails.Rows[i]["color"] = "#8677EF";
                          }
                      }
                  }

              }

解决方案

You can't set colour on a DataTable: it's not a presentation control, which is what handles the actual display - it's just a storage mechanism for the data, it has no idea about the environment in which the data it supplies will be used. You set the colour on the data as part of the display control, and exactly how you do that will depend on teh environment and the control itself.

For example, this shows how to do it for a WinForms DataGridView: Colouring DataGridView Cells According to their Content in WinForms[^]


这篇关于如何根据条件在C#中设置数据表的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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