根据条件在网格视图中显示图像

时间:2011-09-12 13:58:20

标签: c# asp.net gridview rowdatabound

我正在尝试展示 1.如果TimeReceived为空,则为红色,(或) 2.当收到的时间不为空且时间读取为空(或)时为琥珀色 3.绿色当时间读数不为空时

它会抛出错误

Input string was not in a correct format. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error: 


Line 86:         {
Line 87:             Image img = (Image)e.Row.FindControl("image1");
Line 88:             switch (int.Parse(e.Row.Cells[1].Text))
Line 89:             {
Line 90:                 case 0:

我哪里出错,如何根据条件显示图像。我想我还没有正确完成rowdatabound。请帮忙。

4 个答案:

答案 0 :(得分:2)

您可能正在尝试将null或空字符串解析为int。将您的int.Parse行更改为:

switch (int.Parse(string.IsNullOrEmpty(e.Row.Cells[1].Text)?"0":e.Row.Cells[1].Text))

更新:现在你已经粘贴了网格外观的实际图像,我认为Joel Etherton是对的,你试图将Date解析为整数。 Cell [1](假设你左边没有任何不可见的列)是一个Date,而不是一个整数,所以当你尝试int.Parse抛出异常,因为它无法解析它。此外,根据您的条件,您的MyGrid_RowDataBound逻辑不正确。尝试将您的实现更改为此。

protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image img = (Image)e.Row.FindControl("image1");
        //condition for red image; Neither TimeReceived and TimeRead are populated
        if(string.IsNullOrEmpty(e.Row.Cells[1].Text) &&  
           string.IsNullOrEmpty(e.Row.Cells[2].Text))
        {
            img.ImageUrl = "/images/Red.gif";
            img.Visible = true;
        }
        //condition for amber image; TimeReceived not null and TimeRead is null
        else if (!string.IsNullOrEmpty(e.Row.Cells[1].Text) &&  
                 string.IsNullOrEmpty(e.Row.Cells[2].Text))
        {
            img.ImageUrl = "/images/Amber.gif";
            img.Visible = true;
        }
        //condition for green image; TimeReceived not null and TimeRead not null
        else if (!string.IsNullOrEmpty(e.Row.Cells[1].Text) &&  
                 !string.IsNullOrEmpty(e.Row.Cells[2].Text))
        {
           img.ImageUrl = "/images/Green.gif";
           img.Visible = true;
        }
        else //default case
        {
            img.Visible = false;
        }
    }
}

答案 1 :(得分:1)

这是我看到问题发生的地方,坦率地说这段代码有点混乱。

switch(int.Parse(string.IsNullOrEmpty(e.Row.Cells[1].Text)?"0":e.Row.Cells[1].Text))

这里有太多有用的东西。首先,e.Row.Cells [1]看起来它提供了一个DateTime,所以int.Parse在这里使用是绝对错误的。根据您对所需内容的描述,我不知道这将如何以任何方式实现这一目标。

这是我的抨击:

Image img = (Image)e.Row.FindControl("image1");

DateTime received;
DateTime read;

DateTime.TryParse(e.Row.Cells[1].Text, received);   // If exception it will produce DateTime.MinValue
DateTime.TryParse(e.Row.Cells[2].Text, read);

if (received == DateTime.MinValue)
{
    img.ImageUrl = "/images/Red.gif";
}
else if (read == DateTime.MinValue)
{
    img.ImageUrl = "/images/Amber.gif";
}
else
{
    img.ImageUrl = "/images/Green.gif";
}

img.Visible = true;

在适当时使用if语句。你要做的是涉及日期,所以使用日期。简化表达式,使其更具可读性。您不必在一行中完成所有工作。传递给switch语句的表达式一次性完成得太多了。我并不是说它不可能实现,但它会产生很多灰色区域,以确定产生错误的位置。

答案 2 :(得分:1)

我很难理解这些复杂的解决方案,所以我做了一些研究,这是我的解决方案。我把它留在这里,希望能帮到某人

我改变了我的网格代码

<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="false" OnRowCommand="RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Commands">
            <ItemTemplate><asp:ImageButton ID="btnDelete" Visible='<%# ActionPermitted("DELETE") %>' runat="server" ImageUrl="images/bin.png" ToolTip="Delete" CommandName="Delete" CommandArgument='<%# Eval("Id")%>' /></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ASPImage">
            <ItemTemplate><asp:Image runat="server" ID="rowImage" ImageUrl='<%# ImageView(((System.Data.DataRowView) Container.DataItem).Row)%>' /></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="HTMLImage">
            <ItemTemplate><img src='<%# ImageView(((System.Data.DataRowView) Container.DataItem).Row)%>' /></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="SetVisible">
            <ItemTemplate>
                <img src="green.gif" style='display:<%# Eval("aColumn") == "Value1"? "inline":"none" %>' />
                <img src="red.gif" style='display:<%# Eval("aColumn") == "Value2"? "none":"inline" %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

在我的代码背后,我有以下功能

protected boolean ActionPermitted(string action){
  return (action=="Delete" || user == "admin");
}

protected string ImageView(DataRow row){
    if (row["SomeData"] == DBNull.Value){
        return "red.gif";
    } else if (row["SomeData"] != DBNull.Value && row["SomeOtherData"] == DBNull.Value){
        return "amber.gif";
    } else {
        return "green.gif";
    }
}

请注意,这实际上是4种不同的解决方案。

  • 根据函数后面返回的值更改asp按钮的可见性(注意单引号而不是双引号)
  • 将当前DataRow传递给后面的代码并根据该
  • 返回结果
  • 更改HTML标记的属性(可用于将代码中生成的数据插入列中)
  • 显示/隐藏HTML图像(可以是任何标记)

Last TemplateField将回答这个问题。

答案 3 :(得分:0)

我同意Icarus,但如果使用int.TryParse而不是int.Parse会更好。

Image img = (Image)e.Row.FindControl("image1");
int val = 0;
int.TryParse(e.Row.Cells[1].Text , out val);
        switch (int.Parse(e.Row.Cells[1].Text))
        {
            case 0:
                img.ImageUrl = "/images/Red.gif";
                img.Visible = true;
                break;
            case 1:
                img.ImageUrl = "/images/Amber.gif";
                img.Visible = true;
                break;
            case 2:
                img.ImageUrl = "/images/Green.gif";
                img.Visible = true;
                break;               
            default:
                img.Visible = false;
                break;
        }
相关问题