1 Solution
Please refer to the Grid View - Templates demo. There is an image loaded from a database that is displayed in the GridView. You can use a similar approach with two modifications:
- Use the GridViewSettings.SetDataItemTemplateContent method instead of the SetDataRowTemplateContent one;
- Do not use the BinnaryImage and render the <img> tag directly to the template. Use the DataBinder.Eval(c.DataItem, "ImageNameField") expression to render a value of the 'src' attribute.
Updated:
To get ViewContext in a helper method, you can send the ViewContext object from the view to the helper method as an inner parameter:
[C#]public static GridViewSettings CreateOverviewGridViewSettings(ViewContext vc) { ... settings.Columns.Add(column => { column.FieldName = "Flag"; column.Caption = "Flag"; column.Width = 120; column.SetDataItemTemplateContent(c => { string imageUrl = new UrlHelper(HttpContext.Current.Request.RequestContext).Content( String.Format("~/content/images/Flags/{0}.png", DataBinder.Eval(c.DataItem, "Flag"))); vc.Writer.Write(String.Format("<img src=\"{0}\" />", imageUrl)); }); }); ... }
View code:
[C#]@using DevExpressVisiaExample.Views.Home @Html.DevExpress().GridView(WorkerCaseListHelper.CreateOverviewGridViewSettings(Html.ViewContext)).Bind(Model.ClientCases).GetHtml()
-
Hi Vlad
Thanks for the quick response. I'm trying to work through your guidance and I'm struggling to use ViewContext.Writer.Write. It is always showing the error "An object reference is required for the non static field, method or property." I believe this is because I am defining the GridView settings within a Helper File (Static Method) rather than a View. I'm following the Dev Express guidance when needing to Export GridViews which recommends using a Helper class. Should I still be using ViewContext.Writer.Write in this situation or some other approach. Also, I've tried finding out if it's possible to use ViewContext.Writer.Write in a static method but it doesn't seem clear what to do. HELP!
settings.SetDataItemTemplateContent(c =>
{
ViewContext.Writer.Write("<img src='/content/images/" + DataBinder.Eval(c.DataItem, "Flag") + ".png'");
}
);Many thanks
Mitch
-
Hello Mitchell,
Would you please demonstrate implementation details of using GridView in your project? This helps us understand your scenario and provide a more appropriate solution. -
Hi Vladimir
I've put together a small example of what we are trying to achieve by extracting some code from one of our GridView pages. I hope this helps.
Many thanks
Mitch
-
I have examined your sample, modified and attached it to my Answer above. I have also updated the Answer with a short description.
-
Hi Vladimir,
Many thanks for sorting out my problem with a detailed explanation. You mentioned that you had attached a modified sample to the answer but I can't seem to find it?
Mitch
-
Hello Mitchell,
I have re-attached the sample to the parent Answer of this comment tree. Please accept my apologies for the delay.
If you have any other questions, feel free to contact us at any time. -
Hi Vladimir,
We're nearly there!
The only remaining issue is to do with the Partial method I have in my controller that is used to Export the GridView.
It is now broken because it expects a ViewContext to be passed to the Helper Class which was added to get the Writer.Write to work. I understand how to do this from the Partial view by using Html.ViewContext :-
@Html.DevExpress().GridView(DPMSCloud.Views.PeopleListAllClients.AllClientsHelper.OverviewGridViewSettings(Html.ViewContext)).Bind(Model.PeopleWithLatestCase).GetHtml()
but how do you pass a ViewContext from an ActionResult within a Controller:-
[HttpPost]
public ActionResult AllClientsExportTo(string btnExport)
{
return GridViewExportHelper.ExportTypes[btnExport].Method(DPMSCloud.Views.PeopleListAllClients.AllClientsHelper.OverviewGridViewSettings(VIEWCONTEXT NEEDS TO GO HERE!!), repository.GetAllClientsList().ToList());
} -
Hello Mitchell,
GridView cannot export templates. So, there is no reason to render anything to ViewContext in this case. I recommend you skip creation of templates if the GridViewSettings object is being instantiated for data export:
if (vc != null) { column.SetDataItemTemplateContent(c => { string imageUrl = new UrlHelper(HttpContext.Current.Request.RequestContext).Content( String.Format("~/content/images/Flags/{0}.png", DataBinder.Eval(c.DataItem, "Flag"))); vc.Writer.Write(String.Format("<img src=\"{0}\" />", imageUrl)); }); }public ActionResult AllClientsExportTo(string btnExport) { return GridViewExportHelper.ExportTypes[btnExport].Method(DPMSCloud.Views.PeopleListAllClients.AllClientsHelper.OverviewGridViewSettings(null), repository.GetAllClientsList().ToList()); } -
Hi Vladimir
Thankyou SO much for all your help with this issue. I decided to not suppress the Flag column when Exporting but to display the Text data instead of the Flag image so the Flag column puts the name of the flag color such as "Blue" in the Flag column which is a really good compromise.
I'm happy to close this ticket now as it is all working as I want. Once again, many thanks for your excellent help with this issue.
Mitch
-
Hello Mitchell,
Thank you for your kind words. We are happy to hear that our assistance is really useful and required by our clients. This greatly motivates us in our work. :)
Should you face any other issue using our controls, feel free to contact us at any time.
Is your intention to post an answer to your own question?
- If so, then proceed.
- If you simply wanted to post additional information, ask for further clarification, or to just say "Thanks!", please click Leave a Comment.
- If you wish to edit your original question, please use the Edit button in the Toolbox at the top right corner of that entry.
Facebook
Twitter
Google+