Search code examples
c#asp.net-mvcgridviewdevexpressdevexpress-mvc

DevExpress MVC Gridview


Good day guys I'm trying to add a extra column on the grid view with this code. But when i run it. I cant click the button on the grid view. Got any idea what i'm doing wrong. Thanks

        settings.Columns.Add(column =>
        {
            column.FieldName = "Unbound";
            column.Caption = "Action";
            column.UnboundType = DevExpress.Data.UnboundColumnType.Object
            column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.True;
            column.ReadOnly = false;
            column.SetDataItemTemplateContent((c) =>
            {

                Html.DevExpress().Button(b =>
                {
                    b.Name = "btnVE" + c.KeyValue;
                    b.Text = "V/E";
                    b.ClientSideEvents.Click =
                    "function(s, e) { document.location='" + DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" })
                    + "?key=' + s.GetRowKey(e.visibleIndex); }";

                }).GetHtml();
            });
        });

        settings.Columns.Add("Id");
        settings.Columns.Add("Code");
        settings.Columns.Add("CompanyId");
        settings.Columns.Add("Description");
        settings.Columns.Add("ContactPerson");
        settings.Columns.Add("TelNo");
        settings.Columns.Add("Notes");

Update: i found the error on web via web developer tools but i dont know how to fix it Error button hover


Solution

  • Sounds like the problem is originated from s which assigned to button sender on this block instead of GridView row:

    Html.DevExpress().Button(b =>
    {
        b.Name = "btnVE" + c.KeyValue;
        b.Text = "V/E";
        b.ClientSideEvents.Click =
        "function(s, e) { document.location='" + DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" })
        + "?key=' + s.GetRowKey(e.visibleIndex); }"; // ==> 's' refers to button object as sender
    }).GetHtml();
    

    What you should use is GridViewDataItemTemplateContainer object to get KeyValue property for corresponding row, which returns integer value from GridViewBaseRowTemplateContainer:

    column.SetDataItemTemplateContent((c) =>
    {
        Html.DevExpress().Button(b =>
        {
            b.Name = "btnVE" + c.KeyValue;
            b.Text = "V/E";
            b.UseSubmitBehavior = false; // prevent default submit action
            b.EnableClientSideAPI = true; // add this line if not sure
            b.ClientSideEvents.Click =
            "function(s, e) { window.location = '" + DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" })
            + "?key=" + c.KeyValue.ToString() + "'; }";
    
        }).GetHtml();
    });
    

    Or using string.Format() which easier to read:

    b.ClientSideEvents.Click = string.Format("function(s, e) {{ window.location = '{0}?key={1}'; }}", 
                               DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" }), 
                               c.KeyValue.ToString());
    

    Notes:

    1) If you want to get row index, use c.VisibleIndex.

    2) For cross-browser concern, I preferred window.location to document.location as provided here.

    Related issue: GridView - How to define Button inside grid