DataBinder的应用

以前做东西时只知道DataBinder能在html中应用于datagrid或者datalist中的模板列,没想到在cs代码中也能用。

以后再判断gridview列值时,就不用索引了,直接可以用字段名,

cs代码中:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "job_id"));
        if (i % 2 > 0)
        {
            e.Row.BackColor = System.Drawing.Color.Gray;
        }
    }

html代码中:

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
            <Columns >
                <asp:TemplateField >
                <ItemTemplate >
                    <%#DataBinder.Eval(Container.DataItem,"job_id") %>
                 </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

原文地址:https://www.cnblogs.com/wenming205/p/1266570.html