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
It throws an error
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:
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.
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))
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;
}
}
}