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