显示在网格视图图像根据病情 [英] Displaying a image in grid view based on condition

查看:126
本文介绍了显示在网格视图图像根据病情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示
1.红色如果TimeReceived为空,(或)
2.当琥珀收到的时间不是null和时间读为Null(OR)
3.绿色当时间读不为空

I am trying to display 1. Red if the TimeReceived is Null, (or) 2. Amber when Time Received is not null and Time Read is Null (Or) 3. Green When Time read is not null

它抛出一个错误

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。请帮助。

Where I am going wrong, how can I display image based on the condition. I think I haven't done the rowdatabound correctly. Please help.

推荐答案

您可能正在试图解析null或空字符串作为一个int。更改 int.Parse 行:

You are probably trying to parse a null or empty string as an int. Change your int.Parse line to:

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

更新:现在你已经粘贴的网格外观的实际的形象,我想乔尔埃瑟顿是正确的,你试图解析日期为整数。电池[1](假设你没有任何隐形列左侧)是一个日期,而不是​​一个整数,所以当你尝试int.Parse抛出异常,因为它无法解析它。另外,根据你的情况,你的MyGrid_RowDataBound逻辑不正确。试着改变你实现这一点。

UPDATE: Now that you have pasted the actual image of how the Grid looks, I think Joel Etherton is right, you are trying to parse a Date as an integer. Cell[1] (assuming you don't have any invisible columns to the left) is a Date, not an integer so when you try int.Parse throws the exception because it cannot parse it. Also, according to your conditions, your MyGrid_RowDataBound logic is incorrect. Try changing your implementation to this.

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;
        }
    }
}

这篇关于显示在网格视图图像根据病情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆