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

Removing the Add New Item button on Detail Tables

13 Answers 490 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mqsash
Top achievements
Rank 1
mqsash asked on 27 Apr 2013, 12:13 AM
Hi

I am trying to make my radgrid (which is wrapped in a usercontrol) "readonly".
For this I need to remove the "Add New Record" button and the "Edit" and "Delete" columns of the grid.
I have got the following code to work where the "Edit" and "Delete" columns get removed for the master as well as all the detail tables (I have 4 levels of details), but when it comes to removing the "Add New Record" from the command item bar only the button on the MasterTable gets removed.  Any ideas what I am missing here or how I could hide  the button  on detail tables?

protected override void OnPreRender(EventArgs e)
{
  //Complete normal prerender stuff.
  base.OnPreRender(e);
 
  if (ReadOnly)
  {
    MakeGridReadOnly(MasterTableView);
  }
 
}
 
private static void MakeGridReadOnly(GridTableView radGridTableView)
{
  HideAddNewCommandButton(radGridTableView);
  HideEditDeleteColumns(radGridTableView);
 
  foreach (var detailView in radGridTableView.DetailTables )
  {
    MakeGridReadOnly(detailView);
  }
 
}
 
private static void HideAddNewCommandButton(GridTableView radGridTableView)
{
  if (radGridTableView.GetItems(GridItemType.CommandItem).Length <= 0) return;
  var commandItem = radGridTableView.GetItems(GridItemType.CommandItem)[0];

  var newRecordButton = commandItem.FindControl("AddNewRecordButton");
  if (newRecordButton != null)  newRecordButton.Visible = false;
 
  var initInsertButton = commandItem.FindControl("InitInsertButton");
  if (initInsertButton != null) initInsertButton.Visible = false;
 
}
 
private static void HideEditDeleteColumns(GridTableView radGridTableView)
{
  try
  {
    GridColumn editColumn = radGridTableView.Columns.FindByUniqueName("EditCommandColumn");
    if (editColumn != null)
      editColumn.Visible = false;
  }
  catch
  {
  }
 
  try
  {
    GridColumn deleteColumn = radGridTableView.Columns.FindByUniqueName("DeleteCommandColumn");
    if (deleteColumn != null)
      deleteColumn.Visible = false;
  }
  catch
  {
  }
}

13 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Apr 2013, 04:51 AM
Hi,

I guess you want to hide the 'AddNewRecord' button from DetailTable. Please try following solutions.

ASPX:
. . .
<DetailTables>
    <telerik:GridTableView CommandItemDisplay="Top" DataKeyNames="OrderID" Name="DetailTable1" Width="100%">
    <CommandItemSettings ShowAddNewRecordButton="false" />
    . . .

OR

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridCommandItem && e.Item.OwnerTableView.Name == "DetailTable1")
    {
        e.Item.FindControl("InitInsertButton").Parent.Visible = false;
    }
}

Thanks,
Princy.
0
mqsash
Top achievements
Rank 1
answered on 29 Apr 2013, 05:44 PM
Thanks for the response Princy, but unfortunately my situation is a bit different...
My situation is similar to the following post:
http://www.telerik.com/community/forums/aspnet-ajax/ajax/radgrid-addnewrecordbutton-problem-on-prerender-event.aspx#2169542

and the solution suggested by the telerik team in this post works fine as long as the grid does not have any detail tables.

In my case, I have a few grids that have detail tables. What I am seeing in this case is a weird behavior  in my "HideAddNewCommandButton" function from the code pasted in the original post.
When this function is called on the MasterTableView, the "radGridTableView.GetItems(GridItemType.CommandItem)" returns the command item and I can process it and hide the buttons.
When this function is called on the DetailTableView the "radGridTableView.GetItems(GridItemType.CommandItem).Length" is ZERO, i.e it has no command items and therefore does not find the buttons I want to hide.
I confirmed it was the right detail table by looking at the "radGridTableView.Name" property, and also confirmed that the markup has the CommandItemDisplay property set so that it is visible, I mean the command bar and the button is visible on the grid !!. Yet for some reason, the code cannot find the command item in the detail tables!
Any idea why this might be happening or how I can get to the command items of detail tables in the Pre-Render?

private static void HideAddNewCommandButton(GridTableView radGridTableView)
{
  if (radGridTableView.GetItems(GridItemType.CommandItem).Length <= 0) return;
  var commandItem = radGridTableView.GetItems(GridItemType.CommandItem)[0];
 
  var newRecordButton = commandItem.FindControl("AddNewRecordButton");
  if (newRecordButton != null)  newRecordButton.Visible = false;
  
  var initInsertButton = commandItem.FindControl("InitInsertButton");
  if (initInsertButton != null) initInsertButton.Visible = false;
  
}

Thanks
0
mqsash
Top achievements
Rank 1
answered on 29 Apr 2013, 05:51 PM
Instead of getting the CommandItem and doing FindControl and stuff on it, found that the tableView class has a property called "CommandItemSettings.ShowAddNewRecordButton"

Since I already have the detailTableView, all I did was set this to false in the PreRender and viola!! the button is gone, but there's a catch here... the button shows up initially on the grid, but as soon as you click it, it disappears !
As per the post that I used as a reference, the Pre-render event is too late to hide the buttons based on the property...

So is there another event that is earlier on that I can set this property and cause the button to be hidden ? The oninit was too early in my case as I did not yet have the information about whether or not I needed to hide the buttons, but the onLoad() worked perfectly well :)

So now my code looks like this
public Boolean ReadOnly { get; set; }
 
protected override void OnLoad(EventArgs e)
{
  if (ReadOnly)
  {
    MakeGridReadOnly(MasterTableView);
  }
 
  base.OnLoad(e);
}
 
private static void MakeGridReadOnly(GridTableView radGridTableView)
{
  // Hide the Add New Record button
  radGridTableView.CommandItemSettings.ShowAddNewRecordButton = false;
   
  // Hide the Edit and Delete Columns
  HideEditDeleteColumns(radGridTableView);
 
  // Process any child tables of this table.
  foreach (var detailView in radGridTableView.DetailTables )
  {
    MakeGridReadOnly(detailView);
  }
 
}






0
Perry Blanchard
Top achievements
Rank 1
answered on 04 Jun 2015, 02:41 PM

detailTable.CommandItemSettings.ShowAddNewRecordButton = False  is not working.

My  part of aspx page like this.

...........

<MasterTableView runat="server" AllowMultiColumnSorting="True" AutoGenerateColumns="False" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New Department" CommandItemSettings-ShowRefreshButton="false" DataKeyNames="DepartmentID" DataSourceID="SqlDataSourceDepartment" EditMode="EditForms" Name="Department" Width="100%">
<CommandItemSettings ShowAddNewRecordButton="false" />
<DetailTables>
<telerik:GridTableView runat="server" AutoGenerateColumns="False" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New Communication" CommandItemSettings-ShowRefreshButton="false" DataKeyNames="DepartmentID,CommunicationID" DataSourceID="SqlDataSourceCommunications" EditMode="EditForms" Name="Communications" Width="100%">
<ParentTableRelation>
<telerik:GridRelationFields DetailKeyField="DepartmentID" MasterKeyField="DepartmentID" />
</ParentTableRelation>

 ................

My aspx.vb like this.

Private Sub RadGridDepartment_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGridDepartment.ItemDataBound
        If TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.Name = "Communications" Then
            Dim parentItem As GridDataItem = e.Item.OwnerTableView.ParentItem
            Dim myDepartmentID As Int32 = parentItem("DepartmentID").Text
            Dim detailTable As GridTableView = DirectCast(e.Item.OwnerTableView, GridTableView)
            If myDepartmentID = 23 Then
                detailTable.GetColumn("EditCommandColumn2").Display = True
                detailTable.GetColumn("DeleteColumn2").Display = True
                detailTable.CommandItemSettings.ShowAddNewRecordButton = True
            Else                detailTable.GetColumn("EditCommandColumn2").Display = False
                detailTable.GetColumn("DeleteColumn2").Display = False
                detailTable.CommandItemSettings.ShowAddNewRecordButton = False           

            End If
        End If

End Sub

Only detailTable.GetColumn("EditCommandColumn2").Display and detailTable.GetColumn("DeleteColumn2").Display

 working

but, detailTable.CommandItemSettings.ShowAddNewRecordButton = False    not working. Please help.       

 

 

 

 

 

 

0
Eyup
Telerik team
answered on 09 Jun 2015, 11:55 AM
Hi Perry,

You will need to set it individually for every GridTableView:
<telerik:GridTableView ... >
    <CommandItemSettings ShowRefreshButton="false" />

Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Perry Blanchard
Top achievements
Rank 1
answered on 09 Jun 2015, 01:50 PM

Thanks.  I know this part and this is not I want.

I try to hide/display three items in <detailTable> by useing RadGrid1_ItemDataBound depend ParentTable item DepartmentID  in aspx.vb page. Looks like all three items not work right, Sometimes  working right if you expand/collapse one item few times  and sometimes make all wrong if you expand one item while another item is still expand and ........

My three items,

1, detailTable.GetColumn("EditCommandColumn2").Display = True/False
2, detailTable.GetColumn("DeleteColumn2").Display = True/False
3, detailTable.CommandItemSettings.ShowAddNewRecordButton = True/False

My  part of aspx page like this,

............

<telerik:RadGrid ID="RadGridDepartment" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowMultiRowSelection="False" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSourceDepartment" GridLines="None" PageSize="40" ShowStatusBar="true" Skin="Outlook">
< PagerStyle Mode="NumericPages" />
< MasterTableView runat="server" AllowMultiColumnSorting="True" AutoGenerateColumns="False" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New Department" CommandItemSettings-ShowRefreshButton="false" DataKeyNames="DepartmentID" DataSourceID="SqlDataSourceDepartment" EditMode="EditForms" Name="Department" Width="100%">
< CommandItemSettings ShowAddNewRecordButton="false" />
< DetailTables>
< telerik:GridTableView runat="server" AutoGenerateColumns="False" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New Communication" CommandItemSettings-ShowRefreshButton="false" DataKeyNames="DepartmentID,CommunicationID" DataSourceID="SqlDataSourceCommunications" EditMode="EditForms" Name="Communications" Width="100%">

< ParentTableRelation>
< telerik:GridRelationFields DetailKeyField="DepartmentID" MasterKeyField="DepartmentID" />
< /ParentTableRelation>
< HeaderStyle CssClass="InnerHeaderStyle" />
< ItemStyle CssClass="InnerItemStyle" />
< AlternatingItemStyle CssClass="InnerAlernatingItemStyle" />
< Columns>
< telerik:GridEditCommandColumn ButtonType="ImageButton" EditImageUrl="images/editChilden.png" HeaderText="Edit" UniqueName="EditCommandColumn2">
< HeaderStyle Width="20px" />
< ItemStyle CssClass="MyImageButton" />
< /telerik:GridEditCommandColumn>
< telerik:GridBoundColumn UniqueName="CommunicationID" HeaderText="Communication ID"
DataField="CommunicationID" DataType="System.Int32"
FilterControlAltText="Filter Communication ID column" ReadOnly="True" Visible="false"
AllowSorting="False">
< /telerik:GridBoundColumn><telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" ConfirmText="Delete this communication?" HeaderText="Delete" ImageUrl="~/images/deleteChilden2.png" Text="Delete" UniqueName="DeleteColumn2">
< HeaderStyle Width="20px" />
< ItemStyle CssClass="MyImageButton" HorizontalAlign="Center" />
< /telerik:GridButtonColumn>
< /Columns>
< SortExpressions>
< telerik:GridSortExpression FieldName="CommunicationID" SortOrder="Ascending" />
< /SortExpressions>
< EditFormSettings CaptionDataField="CommunicationID" CaptionFormatString="Edit Communication ID = {0}" ColumnNumber="2" EditColumn-ButtonType="PushButton" FormCaptionStyle-ForeColor="#990000" InsertCaption="Add New Communications">
< FormTableStyle CellPadding="2" CellSpacing="0" CssClass="module" GridLines="None" Height="90px" Width="100%" />
< EditColumn CancelText="Cancel" UniqueName="EditCommandColumn1" UpdateText="Update">
< /EditColumn>
< FormTableButtonRowStyle CssClass="EditFormButtonRow" HorizontalAlign="Left" />
< /EditFormSettings>
< /telerik:GridTableView>
< /DetailTables><Columns>

< telerik:GridBoundColumn UniqueName="DepartmentID" HeaderText="Department ID"
DataField="DepartmentID" DataType="System.Int32"
FilterControlAltText="Filter Department ID column" ReadOnly="True" HeaderStyle-Width="100px"
AllowSorting="False">
< /telerik:GridBoundColumn>
< telerik:GridTemplateColumn HeaderText="Department" UniqueName="TemplateColumnDepartment" SortExpression="Department">
< EditItemTemplate>
< asp:TextBox ID="TextBoxDepartment" runat="server" Text='<%# Bind("Department")%>' Width="800"></asp:TextBox>
< asp:RequiredFieldValidator runat="server" ControlToValidate="TextBoxDepartment" ErrorMessage="* required" ForeColor="Red">
< /asp:RequiredFieldValidator>
< /EditItemTemplate>
< ItemTemplate>
< asp:Label ID="LabelDepartment" runat="server" Text='<%# Eval("Department")%>'></asp:Label>
< /ItemTemplate>
< /telerik:GridTemplateColumn>
                        
< /Columns> <SortExpressions>
< telerik:GridSortExpression FieldName="DepartmentID" SortOrder="Ascending" />
< /SortExpressions>
< EditFormSettings CaptionDataField="DepartmentID" CaptionFormatString="Edit Department ID = {0}" ColumnNumber="2" EditColumn-ButtonType="PushButton" FormCaptionStyle-ForeColor="#990000" InsertCaption="Add New Department">
< FormTableStyle CellPadding="2" CellSpacing="0" CssClass="module" GridLines="None" Height="90px" Width="100%" />
< EditColumn CancelText="Cancel" UniqueName="EditCommandColumn1" UpdateText="Update">
< /EditColumn>
< FormTableButtonRowStyle CssClass="EditFormButtonRow" HorizontalAlign="Left" />
< /EditFormSettings>
< /MasterTableView>
< /telerik:RadGrid>  

................

My aspx.vb like this.Private Sub RadGridDepartment_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGridDepartment.ItemDataBound
        If TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.Name = "Communications" Then
            Dim parentItem As GridDataItem = e.Item.OwnerTableView.ParentItem
            Dim myDepartmentID As Int32 = parentItem("DepartmentID").Text
            Dim detailTable As GridTableView = DirectCast(e.Item.OwnerTableView, GridTableView)
            If myDepartmentID = 23 Then
                detailTable.GetColumn("EditCommandColumn2").Display = True
                detailTable.GetColumn("DeleteColumn2").Display = True
                detailTable.CommandItemSettings.ShowAddNewRecordButton = True
            Else                              

                detailTable.GetColumn("EditCommandColumn2").Display = False
                detailTable.GetColumn("DeleteColumn2").Display = False
                detailTable.CommandItemSettings.ShowAddNewRecordButton = False                                  

         End If
        End If

End Sub 

 I don't know if I need use Page_Load , DetailTableDataBind  and  PreRender.Please help. How can I do it if you think I need?  Thanks. 

0
Eyup
Telerik team
answered on 12 Jun 2015, 01:34 PM
Hi Perry,

Please try with DetailTableDataBind and it should work as expected. If ShowAddNewRecordButton does not have effect, you can access the command item of the detailTable using GetItems() method and hide it manually:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/using-the--getitems-getcolumn-and-getcolumnsafe-methods

That should do the trick.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Perry Blanchard
Top achievements
Rank 1
answered on 12 Jun 2015, 03:26 PM

Thanks. I try DetailTableDataBind. It works now. My code likes this,

Private Sub RadGridDepartment_DetailTableDataBind(sender As Object, e As GridDetailTableDataBindEventArgs) Handles RadGridDepartment.DetailTableDataBind
        
            Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
Dim myDepartmentID As String = dataItem("DepartmentID").Text

If myDepartmentID = 23 Then
e.DetailTableView.GetColumnSafe("EditCommandColumn2").Visible = True
e.DetailTableView.GetColumnSafe("DeleteColumn2").Visible = True
e.DetailTableView.CommandItemSettings.ShowAddNewRecordButton = True
Else
e.DetailTableView.GetColumnSafe("EditCommandColumn2").Visible = False
e.DetailTableView.GetColumnSafe("DeleteColumn2").Visible = False
e.DetailTableView.CommandItemSettings.ShowAddNewRecordButton = False
            
        End If
        
End Sub

...........

The only thing is not work right is if you expand one item while another item is still expand, the all items go wrong. So, I have to make only allow one item expand each time.

Private Sub RadGridDepartment_ItemCommand(sender As Object, e As GridCommandEventArgs) Handles RadGridDepartment.ItemCommand
If e.CommandName = RadGrid.ExpandCollapseCommandName Then
Dim item As GridItem
For Each item In e.Item.OwnerTableView.Items
If item.Expanded AndAlso Not item Is e.Item Then
item.Expanded = False
End If
Next item
End If

End Sub

 

I want to know what do I need to do next. Please help.

 

0
Eyup
Telerik team
answered on 17 Jun 2015, 09:01 AM
Hello Perry,

Could you prepare a new very basic runnable web site sample to isolate the grid in question and open a formal support ticket to send it to us along with detailed explanations and sample screenshots demonstrating your exact desired behavior?

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Perry Blanchard
Top achievements
Rank 1
answered on 17 Jun 2015, 02:38 PM

I use DetailTableDataBind(). It works  in hierarchical if there is only one leave detailtable. My question is how do I make my code changes if there are two detailtables in hierarchical. This is my cases. Please see my code.

 

Private Sub RadGridDepartment_DetailTableDataBind(sender As Object, e As GridDetailTableDataBindEventArgs) Handles RadGridDepartment.DetailTableDataBind
If Request.Cookies("MyUserType").Value <> "Account Manager" Then
Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
Dim myDepartmentID As String = dataItem("DepartmentID").Text

If myDepartmentID = Request.Cookies("DepartmentID").Value Then
e.DetailTableView.GetColumnSafe("EditCommandColumn2").Visible = True
e.DetailTableView.GetColumnSafe("DeleteColumn2").Visible = True
e.DetailTableView.CommandItemSettings.ShowAddNewRecordButton = True

  Else
e.DetailTableView.GetColumnSafe("EditCommandColumn2").Visible = False
e.DetailTableView.GetColumnSafe("DeleteColumn2").Visible = False
e.DetailTableView.CommandItemSettings.ShowAddNewRecordButton = False

End If
End If

End Sub

0
Eyup
Telerik team
answered on 22 Jun 2015, 12:17 PM
Hello Perry,

You can also try to switch to HieararchyLoadMode="ServerBind" or "Client" and traverse the detail tables on PreRender to execute your logic:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/hierarchical-grid-types-and-load-modes/how-to/hiding-the-expand/collapse-images-when-no-records

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Perry Blanchard
Top achievements
Rank 1
answered on 22 Jun 2015, 06:28 PM

The link you send to me is not work. Can you  try again.

http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/hierarchical-grid-types-and-load-modes/how-to/hiding-the-expand/collapse-images-when-no-records

 

0
Eyup
Telerik team
answered on 23 Jun 2015, 05:33 AM
Hi Perry,

Sorry for that, here is the correct link:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/hierarchical-grid-types-and-load-modes/how-to/hiding-the-expand-collapse-images-when-no-records

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
mqsash
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
mqsash
Top achievements
Rank 1
Perry Blanchard
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or