Current filter:
                                You should refresh the page.
                                0
                                  • I am showing a list of clients in a GridView and I have a field named Flag. Flag is a String which holds values such as "Red", "Green", Yellow" etc. However, I need to turn this string into an image.

                                    For this purpose I have a range of flag images in the \content\images\ folder in my project. The flags are named Blue.png, Green.png, Yellow.png etc.

                                    Until I get this working, the Flag column is currently displaying the text "Blue" instead of an image of a Blue Flag. So I need to effectively wrap the color value with the path to the flag images i.e. "\content\images\" + Flag + ".png". How do I do this in an MVC GridView.

                                    I've checked your support for over an hour and most examples seem to use images that come from a database, I found a person that asked a similar question but gave up in the end and used a different control , but I'm sure something this simple isn't beyond the abilities of the GridView control?

                                    Finally, I'm up against a bit of a tight deadline with this as I hadn't expected it to be a problem, so I've had to check the Urgent option!

                                You must  log in  or  register  to leave comments

                                1 Solution

                                0

                                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()
                                Q413365.zip
                                Show all comments
                                • Mitchell Thraves 07.02.2012

                                  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.

                                • Mitchell Thraves 07.02.2012

                                  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.

                                • Mitchell Thraves 07.03.2012

                                  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.

                                • Mitchell Thraves 07.04.2012

                                  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());
                                  }
                                • Mitchell Thraves 07.04.2012

                                  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.

                                You must  log in  or  register  to leave comments
                                You must  log in  or  register  to leave an answer

                                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.