比较2个gridview列,如果不同则为文本着色 [英] Compare 2 gridview columns and colour the text if different

查看:56
本文介绍了比较2个gridview列,如果不同则为文本着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个gridview列,只对那些不同的颜色进行着色。



我在下面尝试的只是将整个列着色而不匹配。 />


我的尝试:



I want to compare two gridview columns and only color the ones that are different.

What i tried below just colors the whole column zero without matching.

What I have tried:

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {

               if (DataBinder.Eval(e.Row.DataItem, "RockwellPartno") != DataBinder.Eval(e.Row.DataItem, "Part_Number"))
               {
                   e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
               }
           }
       }

推荐答案

属性e.Row.DataItem在RowDataBound事件被触发之前不可用,在内存中这是在RowCreated事件之后触发



如果您调试这个,您可能会发现这些值为null,请尝试如下所示;

a)为你添加一个断点RowCreated事件处理程序

b)当你在事件处理程序中时,在Visual Studio中打开你的立即窗口&输入以下内容;

The property e.Row.DataItem is not available until the RowDataBound event has fired, from memory this is fired after the RowCreated event

If you debug this you will probably find that these values are null, try as follows;
a) Add a break-point to you RowCreated event handler
b) When you are in the event handler, open your Immediate window in Visual Studio & enter the following;
? DataBinder.Eval(e.Row.DataItem, "RockwellPartno")



这将告诉你表达式的值是什么 - 如果它是null&另一个表达式也为null有你的问题。空值不相等因此表达式为假。



从内存中,要访问数据网格中的值,您需要执行以下任一操作;

a)访问后面代码中字段的内部文本值;


This will tell you what the value of your expression is - if it is null & the other expression is also null there is your issue. Nulls are not equal hence the expression is false.

From memory, to access the values in the datagrid you need to do either of the following;
a) Access the inner text value of the field in the code behind;

e.Row.Cells[0].Text





b)将所需字段添加到ASPX页面中数据网格的DataKeyNames属性中。然后使用以下内容访问代码中的值



b) Add the required fields to the DataKeyNames property of your data-grid in your ASPX page & then access the values in your code behind using the following

GridView1.DataKeys[e.Row.RowIndex]["RockwellPartno"].ToString()





使用调试器&安培;阅读文档。

立即&调试器中的本地窗口将显示您有权访问的值和&将让你弄清楚你能做什么&不能这样做。

MSDN包含数以千计的例子&文档通常非常详细。以下链接是GridView控件的MSDN文档



GridViewRow类(System.Web.UI.WebControls) [ ^ ]



亲切的问候



Use your debugger & read the documentation.
The Immediate & Local windows in your debugger will show you what values you have access to & will allow you to figure out what you can & cannot do.
MSDN contains literally thousands of examples & the documentation is typically very detailed. The below link is the MSDN documentation for the GridView control

GridViewRow Class (System.Web.UI.WebControls)[^]

Kind Regards


这篇关于比较2个gridview列,如果不同则为文本着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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