I'm having some trouble with my code. I'm trying to show a different picture for each case. But the code seems to be flawed, I'm pretty new to C# and ASP.net so I get lost pretty easily. Here's the portion of the code that crashes:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string Class = (e.Row.FindControl("txtClass") as TextBox).Text;
HtmlControl htmctrl = e.Row.FindControl("imgid") as HtmlControl;
switch (Class)
{
case "A1":
{
string Logo = @"C:\Users\Rudra\documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Images\Box_Blue.png";
htmctrl.Attributes.Add("src", Logo);
break;
}
case "A2":
{
string Logo = @"C:\Users\Rudra\documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Images\Box_Green.png";
htmctrl.Attributes.Add("src", Logo);
break;
}
case "A3":
{
string Logo = @"C:\Users\Rudra\documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Images\Box_Red.png";
htmctrl.Attributes.Add("src", Logo);
break;
}
default:
{
string Logo = @"C:\Users\Rudra\documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Images\not-found.png";
htmctrl.Attributes.Add("src", Logo);
break;
}
}
}
The crash Happens on the first line GridViewRow row = GridView1.Rows[e.RowIndex];
. RowIndex
is not recognised (doesn't show up on intellisense).
If you can see where I went wrong, why this crashes and how to fix it I'd be eternally grateful.
You need not convert
GridViewRow row = GridView1.Rows[e.RowIndex];
Instead you can directly use it as
GridViewRow row = e.Row;