C# 如何使用Eval语句检查列值
如何修改此行上的ASP.net GridView:C# 如何使用Eval语句检查列值,c#,asp.net,gridview,C#,Asp.net,Gridview,如何修改此行上的ASP.net GridView:,如果到期日列值早于今天的日期,则该列将检查每行的到期日列中的值,并将背景颜色设为红色 我想使用Eval语句 这就是我所拥有的: using (SqlConnection conn = new SqlConnection(gs)) { try { SqlCommand cmd = new SqlCommand(query, conn); SqlDataAdapter da = new SqlDat
,如果到期日
列值早于今天的日期,则该列将检查每行的到期日
列中的值,并将背景颜色设为红色
我想使用Eval
语句
这就是我所拥有的:
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;
}
}
公共字符串GetTodayDate()
{
return DateTime.Now.ToString(“mm dd yyyy”);
}
我得到以下错误:
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'
我会这样做:
<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>
BackColor="<%# DueDateBG(Eval("Due Date").ToString()) %>"
C#代码:
另一方面,如果您想确保DueDate中的值确实是DateTime,那么您可以像上面Naveen所做的那样使用TryParse。因此,请替换:
OnRowDataBound="yourTasksGV_RowDataBound"
与:
编写一个小助手函数
加价
DateTime dt;
DateTime.TryParse(DataBinder.Eval(e.Row.DataItem, "DueDate").ToString(), out dt)
废话
代码隐藏
<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(对象项)
{
日期时间和日期;
var bg=System.Drawing.Color.White;
if(item!=null&&DateTime.TryParse(item.ToString(),out-dueDate))
{
如果(dueDate
另一个黑客(因为它没有空检查)将是
废话
我知道如何使用它。只想找出Eval
中的条件语句。谢谢。@SearchForKnowledge先生,我会试试BackColor
这是什么的财产?Eval不适用于模板字段的属性,仅适用于其内容的属性controls@Andrei-没有,但它是有标签的。然而,在阅读了其他评论之后,他似乎并没有试图仅仅改变标签的背景。是的。我想把整个细胞涂成红色。但它几乎就在这里:)我得到了这个错误:无法隐式地将类型“object”转换为“System.DateTime”。存在显式转换(是否缺少转换?
@SearchForKnowledge-TryParse确保您在DueDate中拥有的值能够转换为dateTime。如果可以,那么它将把它解析为“dt”。至于你的下一个评论,你确定DueDate的日期值正确吗?如果是的话,你能给出一个被解析的日期的例子吗?您可能需要使用TryParseExact才能指定日期当前使用的确切格式。我需要一个面板而不是标签,因为我希望整个单元格都是红色。该字符串未被识别为有效的日期时间。有一个从索引0开始的未知单词。
错误。它是否检查每行的列?将TryParseExact
与日期格式一起使用。差不多<代码>日期时间.TryParseExact(新项目,“MM-d-yyyy HH:MM:ss tt”,System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None,out dueDate)
注释不用于扩展讨论;这段对话已经结束。
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;
}
}
}
OnRowDataBound="yourTasksGV_RowDataBound"
DateTime dt = (DateTime)DataBinder.Eval(e.Row.DataItem, "DueDate");
DateTime dt;
DateTime.TryParse(DataBinder.Eval(e.Row.DataItem, "DueDate").ToString(), out dt)
<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;
}
<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>