如何使用Eval语句检查列值

时间:2015-05-22 14:02:18

标签: c# asp.net gridview

<asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="You currently have no tasks assigned to you" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated">
    <Columns>
<asp:TemplateField HeaderStyle-Width="2%">
    <ItemTemplate>
        <asp:ImageButton ImageUrl="~/theImages/Dependencies.png" CssClass="gvTaskDep btnShowDepend" runat="server" ID="btnShowDepend" OnCommand="btnShowDepend_Command" CommandName="TaskDepend" AlternateText='<%#Eval("Object") + "," + Eval("FK") %>' CommandArgument='<%#Eval("Object") + "," + Eval("FK") %>' ToolTip="Click to view Dependencies" />
    </ItemTemplate>
</asp:TemplateField>
        <asp:HyperLinkField HeaderStyle-Width="16%" Target="_self" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
        <!-- ADD ANOTHER COLUMN HERE -->
        <asp:BoundField HeaderStyle-Width="10%" DataField="Workgroup" HeaderText="Workgroup" SortExpression="Workgroup" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField HeaderStyle-Width="7%" DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField HeaderStyle-Width="7%" DataField="Due Date" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn" />
    </Columns>
</asp:GridView>

我正在填充我的GridView:

using (SqlConnection conn = new SqlConnection(gs))
{
    try
    {
        SqlCommand cmd = new SqlCommand(query, conn);

        SqlDataAdapter da = new SqlDataAdapter(query, conn);

        myDataSet = new DataSet();
        da.Fill(myDataSet);

        myDataView = new DataView();
        myDataView = myDataSet.Tables[0].DefaultView;

        yourTasksGV.DataSource = myDataView;
        yourTasksGV.DataBind();

    }
    catch (Exception ex)
    {
        string error = ex.Message;
    }
}

如何在此行修改我的ASP.net GridView:<!-- ADD ANOTHER COLUMN HERE -->,其中列将检查每行中Due Date列中的值,并使背景颜色为红色,如果Due Date 1}}列值在今天的日期之前。

我想使用Eval声明。

这就是我所拥有的:

public string GetTodayDate()
{
    return DateTime.Now.ToString("mm-dd-yyyy");
}

<asp:TemplateField HeaderText="V%">
    <ItemTemplate>
        <!-- FILL ENTIRE CELL? -->
        <asp:Panel ID="pnlTaskStats" Width="100%" Height="100%" runat="server" BackColor="<%# (Eval("Due Date").ToString() < GetTodayDate()) ? "#C00000" : "" %>"></asp:Panel>
    </ItemTemplate>                   
</asp:TemplateField>

我收到以下错误:

`Operator '<' cannot be applied to operands of type 'string' and 'string'`

3 个答案:

答案 0 :(得分:1)

我做这样的事情:

C#代码:

public Color DueDateBG (DateTime DueDate)
{
    Color clr = //whatever default colour

    if(DueDate < DateTime.Today) 
    {
        //Any further logic for deciding on colour
    }

    return clr;
}

ASP.NET代码:

BackColor="<%# DueDateBG(Eval("Due Date").ToString()) %>" 

请注意,这只会更改标签的背景,而不会改变整个单元格,因为您的进一步评论意味着您的目标。

使用RowDataBound,您可以访问整行并像这样更改它:

protected void yourTasksGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DateTime dt = (DateTime)DataBinder.Eval(e.Row.DataItem, "DueDate");
        if(dt < DateTime.Today) 
        {
             e.Row.BackColor = System.Drawing.Color.Red;
             //or do it for a specific cell
             e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
        }
    }
}

ASP.NET代码将事件添加到GridView:

OnRowDataBound="yourTasksGV_RowDataBound"

另外,如果您想确保DueDate中的值确实是DateTime,那么您可以使用Naveen上面的TryParse。所以替换:

DateTime dt = (DateTime)DataBinder.Eval(e.Row.DataItem, "DueDate");

使用:

DateTime dt;
DateTime.TryParse(DataBinder.Eval(e.Row.DataItem, "DueDate").ToString(), out dt)

答案 1 :(得分:1)

编写一个小帮助函数。

标记

<asp:TemplateField>
    <ItemTemplate>
        <asp:Panel ID="lblPD" runat="server" Text="blah" BackColor='<%# SetBackGroundColor(Eval("[Due Date]")) %>'>
            blah blah
        </asp:Panel>
    </ItemTemplate>
</asp:TemplateField>

代码隐藏

protected System.Drawing.Color SetBackGroundColor(object item)
{
    DateTime dueDate;
    var bg = System.Drawing.Color.White;
    if (item != null && DateTime.TryParse(item.ToString(), out dueDate))
    {
        if (dueDate < DateTime.Today)
        {
            bg = System.Drawing.Color.Red;
        }
    }
    return bg;
}

另一个黑客(丑陋的cos没有空检查)将是

<asp:TemplateField>
    <ItemTemplate>
        <asp:Panel ID="lblPD1" runat="server" Text="blah1" 
            BackColor='<%# DateTime.Parse(Eval("[Due Date]").ToString()) < DateTime.Today ? System.Drawing.Color.Red : System.Drawing.Color.Black %>'>
            blah
        </asp:Panel>
    </ItemTemplate>
</asp:TemplateField>

答案 2 :(得分:0)

 <asp:TemplateField HeaderText="Edit">
                                <ItemTemplate>
                                    <a href='Registration.aspx?key=<%#Eval("yourTasksGV_Sorting") %>'>Edit</a>
                                </ItemTemplate>