I am trying to display an image in a gridview, however, the columns that are being displayed in the gridview are all dynamically displayed(not generated).
I have looked at this post already, but it doesn’t seem to work in my case:
How to access ImageUrl property of the Image Control placed inside a ItemTemplate in the GridView
What I have is a gridview with these columns:
<Columns>
<telerik:GridBoundColumn HeaderText="Custom1" HeaderStyle-VerticalAlign="Bottom" DataField="Custom1" SortExpression="Custom1"/>
<telerik:GridDateTimeColumn HeaderText="Custom2" HeaderStyle-VerticalAlign="Bottom" DataField="Custom2" SortExpression="Custom2"/>
<telerik:GridCheckBoxColumn HeaderText="Custom3" HeaderStyle-VerticalAlign="Bottom" DataField="Custom3" SortExpression="Custom3"/>
<telerik:GridTemplateColumn HeaderText="Custom4" HeaderStyle-VerticalAlign="Bottom" DataField="Custom4"><itemtemplate><asp:Image runat="server" ID="Custom4_pic"/></itemtemplate></telerik:GridTemplateColumn>
the four columns are repeated changing the Custom# value until it reaches 48.
I then proceed to hiding all unnecessary columns and only displaying the columns I need.(This is because a user can define the order of the columns to appear, and hardcoding it all was the easiest way I could implement it quickly).
Everything is displayed properly, however, I can’t seem to figure how to set the ImageURL for the image control. For the rest of the columns this is how I am doing it:
dt = new DataTable();
// Do some stuff...
if (aProperty.WebControlType == EntityPropertyWebControlType.CheckboxYN || aProperty.WebControlType == EntityPropertyWebControlType.OptionYN)
{
dt.Columns.Add("Custom" + ((i * 4) - 1).ToString(), typeof(bool));
break;
}
else if (aProperty.WebControlType == EntityPropertyWebControlType.Date)
{
dt.Columns.Add("Custom" + ((i * 4) - 2).ToString(), typeof(DateTime));
break;
}
else if (aProperty.WebControlType == EntityPropertyWebControlType.Image)
{
// Not sure what to do here...
break;
}
else
{
dt.Columns.Add("Custom" + ((i*4) - 3).ToString(), typeof(string));
break;
}
// Do more stuff...
// Based on type I will set the values to equal something.. so for example
dr["Custom" + aProperty.LabelColumnNo.ToString()] = "Text" or bool or DateTime
// However I'm not sure what to do for Image...
// and then I return the datatable to be binded to the gridview
Any tips would be great, thanks!
I have solved the issue.
I replaced the template columns with GridImageColumns, then in my codebehind, I add Columns to my datatable of type string, I then used an image handler, and set the string to the image handler page.