Ticket Q470777
Visible to All Users

How to bind Columns to GridView according to collection

created 12 years ago

HI Team,

I would like to bind the columns for the Gridveiw according to the collection what i am passing from the controller.
Below is the code snippet shows like i have hardcoded the column name like
"settings.Columns.Add("Name").SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
settings.Columns.Add("Amount");".

However how i need achieve the functionality without mentioning the column names in the partial view. My intension to do is i would like to create a partial view with complete dynamic GridView.

In Controller screen:
----------------------------
objCustomer = new Customer();
objCustomer.Code = 200;
objCustomer.Name = "Laptop";
objCustomer.Amount = 40000;
customers.Add(objCustomer);
return View("Index", customers);

In Partial View
-------------------
@Html.DevExpress().GridView(
settings =>
{
settings.Name = "myGridView";
settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPart" };
settings.Width = 450;
settings.Columns.Add("Name").SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
settings.Columns.Add("Amount"** );
settings.SettingsPager.PageSize = 1;
settings.SettingsPager.FirstPageButton.Visible = true;
settings.SettingsPager.LastPageButton.Visible = true;
settings.CommandColumn.Visible = true;
var column = settings.Columns.Add("Code", MVCxGridViewColumnType.HyperLink);
DevExpress.Web.ASPxEditors.HyperLinkProperties properties = column.PropertiesEdit as DevExpress.Web.ASPxEditors.HyperLinkProperties;
String urlFormatString = Url.Action("EditingEdit", "Home", new RouteValueDictionary(new
{
Code = "{0}"
}));
properties.NavigateUrlFormatString = HttpUtility.UrlDecode(urlFormatString);
properties.TextField = "Code";
}).Bind(Model).GetHtml()

Answers approved by DevExpress Support

created 12 years ago (modified 12 years ago)

Hello Pradeep,
I am afraid the GridView does not support the functionality you are looking for. You can only define these settings in a helper class and then assign this helper to the GridView. However, code will be approximately the same.
UPDATED:
If you want to implement the so-called AutoGenerateColumns option (i.e, to force the GridView to create columns automatically according to the public properties of the type/element in the provided Model), simply remove the columns' definition from the PartialView (and the helper class as well).
Please take a look at the E20054 - GridView - How to add a column if the AutoGenerateColumns property is set to true Code Central example, which may be helpful in your scenario (just exclude the command column definition).

C#
@Html.DevExpress().GridView(settings => { settings.Name = Name_Here; settings.CallbackRouteValues = new { Controller = Controller_Name_Here, Action = Action_Name_Here }; }).Bind(Model).GetHtml()
    Show previous comments (1)

      Hello Pradeep,
      If you want to implement the so-called AutoGenerateColumns option (i.e, to force the GridView to create columns automatically according to the public properties of the type/element in the provided Model), simply remove the columns' definition from the PartialView (and the helper class as well).
      Please take a look at the E20054 - GridView - How to add a column if the AutoGenerateColumns property is set to true Code Central example, which may be helpful in your scenario (just exclude the DataBound handler):

      C#
      @Html.DevExpress().GridView(settings => { settings.Name = Name_Here; settings.CallbackRouteValues = new { Controller = Controller_Name_Here, Action = Action_Name_Here }; }).Bind(Model).GetHtml()

      I have updated our Answer accordingly. Please let me know if this is what you are looking for.

        Hi Mike,
        Thanks for reply. and as per your instruction its working fine.
        However the first column in the GridView should be the ActionLink or HyperLink column. How i can add ActionLink or HyperLink for the first column in the GridView ?

          Hello,
          When using the AutoGenerateColumns option, data columns become available after the internal grid has been already bound. It is too late according to the grid life cycle to change grid/column settings because the next stage is the final rendering.
          It is possible to access the GridViewDataColumn (a parent class) instance from the bound column collection and modify its DataItemTemplate. See the attached project.
          However, this approach is some kind of mixing the WebForms and MVC concepts in the same area. Would you please clarify what kind of actions the first column should perform? I will do my best to provide you with an appropriate solution.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.