比较GridView上的值并根据条件设置背景色单元格

时间:2019-03-28 09:11:07

标签: c# gridview

aspnet上标记页面中的 GridView 从下面的sql查询中填充。

第2行中,我有一个不能超过的极限值,在这种情况下,该值为225。

第1行中,我在数据库中注册了一个值,在这种情况下,该值为6534。

现在,我需要比较这些值,如果第1行的值大于第2行的值,则需要在其中设置标签的背景色 GridView ,它必须为红色。

我曾想用 RowDataBound 解决,但没有成功,因为我有错误:

  

索引超出范围。必须为非负数且小于   集合参数名称:index

请帮帮我。

我的下面的代码。

SQL查询

mysql> SELECT
    H2
FROM
    (
        SELECT
            H2
        FROM
            `listValue`
        UNION ALL
            SELECT
                H2_PPM
            FROM
                `listfiles`
            WHERE
                ID = 17819
    ) tbl
GROUP BY
    H2
HAVING
    count(*) = 1;
+------+
| H2   |
+------+
| 225  |
| 6534 |
+------+
2 rows in set

OnRowDataBound

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int ValH2 = Convert.ToInt32(gv.Rows[0].Cells[0].Text);

        //tried also int ValH2 = Convert.ToInt32(gv.Rows[1].Cells[1].Text);
    }
}

GridView标签

<asp:TemplateField HeaderText="H2" ItemStyle-CssClass="ddl_Class_new" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lbH2" runat="server" Text='<%# String.Format("{0:N0}", Eval("H2"))%>' ></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

编辑#02

**Edit #02**

编辑#01

Line 275:        if (e.Row.RowType == DataControlRowType.DataRow)
Line 276:        {
Line 277:            int ValH2 = Convert.ToInt32(gv.Rows[0].Cells[0].Text);
Line 278:        }
Line 279:    } 

Index was out of range. Must be non-negative and less than the size of the collection parameter name:index    Line: 277 

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection parameter name:index]
   System.Collections.ArrayList.get_Item(Int32 index) +14540872
   System.Web.UI.WebControls.GridViewRowCollection.get_Item(Int32 index) +19
   Default.gv_RowDataBound(Object sender, GridViewRowEventArgs e) in c:\inetpub\wwwroot\aspnet\Default.aspx.cs:277
   System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) +137
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, 
   DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, 
   PagedDataSource pagedDataSource) +262
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +5059
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +97
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +18
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +165
   Default.BindData() in c:\inetpub\wwwroot\aspnet\Default.aspx.cs:86
   Default.btnInc_Click(Object sender, ImageClickEventArgs e) in c:\inetpub\wwwroot\aspnet\Default.aspx.cs:197
   System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +141
   System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +186
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5028 

0 个答案:

没有答案