简体   繁体   中英

How add last column in GridView

I need add last column "action", and set in cell href <a href="/?page=@Id">details</a> how do this in devexpress?

@Html.DevExpress().GridView(
    settings =>
    {
        settings.Name = "gvGrouping";

        settings.Columns.Add("Id");//not visible
        settings.Columns.Add("Service", "Сервис");

    settings.Columns.Add("Action", "Action");//I have error

    }
    )

How I usually do it (don't know if it's THE best way, but it does the job):

 settings.Columns.Add(column =>
                                {
                                 column.FieldName = "Id";
                                 column.Caption = " ";
                                 column.Settings.AllowAutoFilter = DefaultBoolean.False;
                                 column.Settings.AllowDragDrop = DefaultBoolean.False;
                                 column.Settings.AllowSort = DefaultBoolean.False;
                                });
 settings.CustomColumnDisplayText = (sender, e) =>
                                          {
                                                if (e.Column.FieldName == "Id")
                                                {
                                                     e.DisplayText = // put your actionlink here                                                                                       
                                                }
                                          };

This gives you the ability to put whetever you like in there (an image, a URL, etc..). You can access the value of the Id by calling 'e.Value' or args.GetFieldValue("") for another property. Or you can just access the model (Model.blabId)..

Hope this helps

UPDATE:

I don't know how you've structured your grid, but this is how it should be (I think there is something wrong with the way you're calling the grid, that's why you're getting the error):

 Html.DevExpress().GridView(settings =>
    {
       //all your settings stuff in here
    }).Bind(Model.YourList).Render();

You could also use SetDataItemTemplateContent

@Html.DevExpress().GridView(
    settings =>
    {
       var actionCol = settings.Columns.Add("Action");
       actionCol.SetDataItemTemplateContent(() =>
       { 
          Html.ActionLink("Action", "SomeAction", "SomeController", new { myParam =    Model.Param });
    } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM