This is a migrated thread and some comments may be shown as answers.

Enable Disable Add Edit Button in Rad Grid

7 Answers 1542 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sukhbinder
Top achievements
Rank 1
Sukhbinder asked on 25 Jun 2012, 07:41 AM
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

7 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 25 Jun 2012, 08:58 AM
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.
0
Sukhbinder
Top achievements
Rank 1
answered on 25 Jun 2012, 10:50 AM

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

0
Peter
Top achievements
Rank 1
answered on 23 May 2014, 02:38 AM
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?
0
Peter
Top achievements
Rank 1
answered on 23 May 2014, 05:29 AM
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);
                            }
                        }
                    }
                }
            }
        }



0
Shinu
Top achievements
Rank 2
answered on 23 May 2014, 08:40 AM
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
0
Peter
Top achievements
Rank 1
answered on 26 May 2014, 08:34 AM
I'd just like a a Grid-level flag like RadGrid.HideEditingButtons :)
0
Dan
Top achievements
Rank 1
answered on 11 Mar 2016, 09:38 PM

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;
}
    
}
}

Tags
General Discussions
Asked by
Sukhbinder
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Sukhbinder
Top achievements
Rank 1
Peter
Top achievements
Rank 1
Dan
Top achievements
Rank 1
Share this question
or