<asp:TemplateField HeaderText="Status" ItemStyle-Width="15%">
<ItemTemplate>
<asp:Image ID="Status" runat="server" />
</ItemTemplate>
</asp:TemplateField>
我在网格视图中有这个列,并且我的表中没有列作为状态,我使用rowdatabound来显示基于其他列中的值的图像,
protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image img = (Image)e.Row.FindControl("Status");
DateTime received;
DateTime read;
DateTime.TryParse(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "TimeReceived")), out received);
DateTime.TryParse(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "TimeRead")), out read);
if (received == DateTime.MinValue)
{
img.ImageUrl = "Styles/Images/red.png";
img.ToolTip = "Message Not Received";
}
else (read == DateTime.MinValue)
{
img.ImageUrl = "Styles/Images/amber.png";
img.ToolTip = "Message Received";
}
img.Visible = true;
}
}
如何对状态列进行排序,其他列我将排序表达式作为列名称,我正在对其进行排序。但对于带有图像的列,我该如何进行排序。
答案 0 :(得分:1)
我通常已经在后端设置中拥有了一个属性来保存“状态”,然后您可以对其进行排序并仍然应用您的UI逻辑。
<asp:TemplateField HeaderText="Status" SortExpression="MessageStatus">
<ItemTemplate>
<asp:Image ID="Status" runat="server" />
</ItemTemplate>
</asp:TemplateField>
public YourObject
{
public string MessageStatus {get; set;}
..........
}
在调用查询时将逻辑放在业务层中:
//call db
//fill object
//while filling object
DateTime received = this.TimeReceived
DateTime read = this.TimeRead
if (received == DateTime.MinValue)
{
MessageStatus = "Message Not Received";
}
else (read == DateTime.MinValue)
{
MessageStatus = "Message Received";
}
<强>的RowDataBound:强>
protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image img = (Image)e.Row.FindControl("Status");
string messsageStatus = DataBinder.Eval(e.Row.DataItem, "MessageStatus") as string;
if (messsageStatus == "Message Not Received")
{
img.ImageUrl = "Styles/Images/red.png";
img.ToolTip = messsageStatus ;
}
else if (messsageStatus == "Message Received")
{
img.ImageUrl = "Styles/Images/amber.png";
img.ToolTip = messsageStatus ;
}
img.Visible = true;
}
}
答案 1 :(得分:0)
在模板中添加SortExpression,为您可以排序的其他字段命名。
<asp:TemplateField HeaderText="Status" SortExpression="SomeOtherColumn">
<ItemTemplate>
<asp:Image ID="Status" runat="server" />
</ItemTemplate>
</asp:TemplateField>