Wednesday, April 11, 2007 12:56 PM
by
mitchell
Dynamic Images for ImageField in GridView
I'm not sure how other people have solved this, but here's what I did.
Problem
I have a GridView in which I want to display two columns, CaseId and Status. CaseId is a Hyperlink field which, when clicked, opens a page displaying all of the relevant information associated with that Id (there are reasons not to use the detailed view). Status is an ImageField. The data values are as follows: CaseId is a unique field given to me from the database. Status is a numeric identifier, 1, 2, or 3 (Active, Pending, or Closed). Depending on the status value, I want to display a different image in the ImageField. However, there didn't seem to be an easy way of getting to the ImageField.DataImageUrlField.
Solution
My solution was to intervene in the GridView_RowDataBound event like this:
protected void gridAlerts_RowDataBound(object sender, GridViewRowEventArgs e){
int status = 0;
if (e.Row.RowType == DataControlRowType.DataRow) {
Int32.TryParse(((CaseManagement.HotAlert)e.Row.DataItem).Status.ToString(), out status);
switch (status) {
case 1:
e.Row.Cells[1].Text = "<img src=\"images/red_flag.gif\"/>";
break;
case 2:
e.Row.Cells[1].Text = "<img src=\"images/yellow_flag.gif\"/>";
break;
case 3:
e.Row.Cells[1].Text = "<img src=\"images/green_flag.gif\"/>";
break;
}
}
}
And that worked like a charm.