Enable Disable Add Edit Button in Rad Grid

8 posts, 1 answers
  1. Sukhbinder
    Sukhbinder avatar
    7 posts
    Member since:
    May 2012

    Posted 25 Jun 2012 Link to this post

    Hi Team,

    I have a requirement where I need to hide the Add New Record Button and disable Edit button through criteria at code behind when the grid is bound.

    Regards
    SS Bhogal
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 25 Jun 2012 Link to this post

    Hi,

    Try the following code snippet to make the Edit button and Add New Record Button hidden.

    C#:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if ("your Condition")
        {
            foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
            {
                ((LinkButton)dataItem["EditCommandColumnUniqueName"].Controls[0]).Enabled = false;
                GridCommandItem cmdItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
                ((LinkButton)cmdItem.FindControl("InitInsertButton")).Visible = false;
                ((Button)cmdItem.FindControl("AddNewRecordButton")).Visible = false;
            }
        }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sukhbinder
    Sukhbinder avatar
    7 posts
    Member since:
    May 2012

    Posted 25 Jun 2012 Link to this post

    Thanks Shinu,

    Could do it and could remove the Add New Record Button too - 

    rg_ManageTransaction.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = true;
    rg_ManageTransaction.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;

    Regards

    SS Bhogal

  5. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 22 May 2014 in reply to Sukhbinder Link to this post

    I have read-only and read-write users that will be using the system - is there any easier way to implement read-only on the grid then using these approaches to hide buttons?
  6. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 23 May 2014 in reply to Peter Link to this post

    If you're doing inline editing, maybe it's just easier to hide the "Insert/Update" button? Is there a quick way to do that? This is handy, because you might have Display=False on a column to save real-estate on the display mode, but show extra columns in "edit" mode. 

    What I've got so far - as you can see, it's stupidly complicated, just to stop certain people editing! Any better ways to do this?

    protected void rgAgreements_PreRender(object sender, EventArgs e)
            {
     GridTableView mtvAgreements = rgAgreements.MasterTableView;
                if (!userCanEdit)
                {
                    GridCommandItem cmdItem = (GridCommandItem)mtvAgreements.GetItems(GridItemType.CommandItem)[0];
                    ((LinkButton)cmdItem.FindControl("InitInsertButton")).Visible = false;
                    ((Button)cmdItem.FindControl("AddNewRecordButton")).Visible = false;
                    LoopHierarchyRecursive(rgAgreements.MasterTableView);
                }

     foreach (GridDataItem item in mtvAgreements.Items)
                {
                  // I'm doing other stuff here

                    if (!userCanEdit)
                    {
                        DisableItemEditing(item);
                    }
                }

     private static void DisableItemEditing(GridDataItem item)
            {
                ((ImageButton)item["GridEditCommandColumn"].Controls[0]).Visible = false;
                ((ImageButton)item["btnDelete"].Controls[0]).Visible = false;
            }

            private static void LoopHierarchyRecursive(GridTableView gridTableView)
            {
                foreach (GridNestedViewItem nestedViewItem in gridTableView.GetItems(GridItemType.NestedView))
                {
                    // skip the items if not expanded, or tables not bound
                    if (nestedViewItem.NestedTableViews.Length > 0)
                    {
                        foreach (GridTableView gtv in nestedViewItem.NestedTableViews)
                        {
                            if (gtv.Items.Count > 0)
                            {
                                var commandItems = gtv.GetItems(GridItemType.CommandItem);
                                if (commandItems.Length > 0) // Have to check this on a collapse
                                {
                                    GridCommandItem cmdItem = (GridCommandItem)commandItems[0];
                                    ((Button)cmdItem.FindControl("AddNewRecordButton")).Visible = false;

                                    foreach (GridDataItem item in gtv.Items)
                                    {
                                        DisableItemEditing(item);
                                    }

                                    LoopHierarchyRecursive(gtv);
                                }
                            }
                        }
                    }
                }
            }

     private static void DisableItemEditing(GridDataItem item)
            {
                ((ImageButton)item["GridEditCommandColumn"].Controls[0]).Visible = false;
                ((ImageButton)item["btnDelete"].Controls[0]).Visible = false;
            }

            private static void LoopHierarchyRecursive(GridTableView gridTableView)
            {
                foreach (GridNestedViewItem nestedViewItem in gridTableView.GetItems(GridItemType.NestedView))
                {
                    // skip the items if not expanded, or tables not bound
                    if (nestedViewItem.NestedTableViews.Length > 0)
                    {
                        foreach (GridTableView gtv in nestedViewItem.NestedTableViews)
                        {
                            if (gtv.Items.Count > 0)
                            {
                                var commandItems = gtv.GetItems(GridItemType.CommandItem);
                                if (commandItems.Length > 0)
                                {
                                    GridCommandItem cmdItem = (GridCommandItem)commandItems[0];
                                    ((Button)cmdItem.FindControl("AddNewRecordButton")).Visible = false;

                                    foreach (GridDataItem item in gtv.Items)
                                    {
                                        DisableItemEditing(item);
                                    }

                                    LoopHierarchyRecursive(gtv);
                                }
                            }
                        }
                    }
                }
            }



  7. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 23 May 2014 in reply to Peter Link to this post

    Hi  Peter,

    I'm not sure about your requirement, if you want to hide the AddNewRecord button and EditButton for a detailtable, please try the following code snippet.

    C#:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
     foreach (GridDataItem dataItem in RadGrid1.Items)
       {
        if (dataItem.OwnerTableView.Name == "TableName")
          {
            dataItem.OwnerTableView.CommandItemDisplay = GridCommandItemDisplay.None;
            dataItem.OwnerTableView.GetColumn("EditColumnUniqueName").Visible = false;
            dataItem.OwnerTableView.Rebind();
          }
        }
     }

    Thanks,
    Shinu
  8. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 26 May 2014 in reply to Shinu Link to this post

    I'd just like a a Grid-level flag like RadGrid.HideEditingButtons :)
  9. Dan
    Dan avatar
    29 posts
    Member since:
    Oct 2013

    Posted 11 Mar Link to this post

    I have found this to be useful if you have more than one RadGrid that you have to "Disable."
    Other Advantages:

    - Eliminates the unnecessary looping.
    - Works for all types of CommandItemDisplay
    Add this to your grid Pre_Render event handler

    private void DisableGridEditing(Telerik.Web.UI.RadGrid myRadGrid)
    {
    if (yourCondition)
    {
    GridItem[] cmdItems = myRadGrid.MasterTableView.GetItems(GridItemType.CommandItem);
    foreach (GridItem cmdItem in cmdItems)
    {
    ((LinkButton)cmdItem.FindControl("InitInsertButton")).Visible = false;
    ((Button)cmdItem.FindControl("AddNewRecordButton")).Visible = false;
    ((LinkButton)cmdItem.FindControl("SaveChangesButton")).Visible = false;
    ((Button)cmdItem.FindControl("SaveChangesIcon")).Visible = false;
    ((LinkButton)cmdItem.FindControl("CancelChangesButton")).Visible = false;
    ((Button)cmdItem.FindControl("CancelChangesIcon")).Visible = false;
    }
        
    }
    }

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017