Search code examples
devexpressaspxgridview

How to hide conditionally the New button when ShowNewButtonInHeader is used?


Summary: I need to show the New button only for users that have the role (here manager). I do not know how to do it when the new button is placed in the command column header.

Details: I am using DevExpress 14.2. When the New button in the ASPxGridView is used in the command column at the row, I can hide it in the CommandButtonInitialize event handler:

protected void gv_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
{
    ASPxGridView grid = sender as ASPxGridView;
    switch (e.ButtonType)
    {
        case ColumnCommandButtonType.New:
            e.Visible = Page.User.IsInRole("manager");
            break;
    ...
    }

However, when the property ShowNewButtonInHeader is set to true, it should probably be done differently. I have found some hints to use Init or Load items. However, I am not sure how to do that.


Solution

  • I have found the way. If you know a better approach, I will accept your answer:

    protected void gv_Init(object sender, EventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;
        foreach (GridViewColumn col in grid.Columns)
        {
            if (col is GridViewCommandColumn && !Page.User.IsInRole("manager"))
            {
                (col as GridViewCommandColumn).ShowNewButtonInHeader = false;
                break;
            }
        }
    }