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

"Add new record" button hide when 1 Row is available

2 Answers 139 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mario
Top achievements
Rank 1
Mario asked on 27 Mar 2012, 11:06 AM
Hello,
I have 2 questions.
Q 1.) I have a MasterTableView and a DetailTables. This works fine.
<MasterTableView DataKeyNames="cid" DataSourceID="SqlDataSource1">
    <DetailTables>
        <telerik:GridTableView runat="server" DataKeyNames="cid"
            DataSourceID="SqlDataSource2" AllowPaging="False" CommandItemDisplay="Top"
            AllowAutomaticInserts="True" AllowAutomaticUpdates="True">
            <ParentTableRelation>
                <telerik:GridRelationFields DetailKeyField="cid" MasterKeyField="cid" />
            </ParentTableRelation>
            ...

When i have 1 DataRow in the DetailTable, how can i hide the "Add new record" in the DetailTable header?

Q 2.) I will use a new button in the DetailTable in the DataRows. This will open a new Window with Javascript. I need 2 parameters from the MasterTableView Columns. This columns are "visible=false" in the grid. How can I access this data via Javascript?

Best regards
Reiner

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 27 Mar 2012, 03:07 PM

Hi Mario,

1)Try the following method to hide the AddNewRecord button in Detail table based on the condition.

C#:

C#:
 
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
  foreach (GridDataItem item in RadGrid1.Items)
  {
     if (item.Expanded)
     {
       GridTableView detailTable = (GridTableView)item.ChildItem.NestedTableViews[0];
       if (detailTable.Items.Count == 1)
       {
          GridCommandItem itm = (GridCommandItem)detailTable.GetItems(GridItemType.CommandItem)[0];
          itm.FindControl("AddNewRecordButton").Parent.Visible = false;     
       }
     }
   }
}

2)For your second requirement I have added a sample code snippet. I have one button in DetailTable and I am attaching its onclick client event from code behind and passing the parent row index as the parameter. In onclick event hander I could able to get the parent table cell value as below. Please note in order to get the column from code behind you need to set the Display property of grid column instead of Visible property.

ASPX:

<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False" onprerender="RadGrid1_PreRender"
           onitemcreated="RadGrid1_ItemCreated" >
             <MasterTableView  Name="Master" ClientDataKeyNames="CustomerID" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" CommandItemDisplay="Top">
               <DetailTables>
                   <telerik:GridTableView   Name="GridTableView1" DataKeyNames="OrderID" DataSourceID="SqlDataSource2"  runat="server" CommandItemDisplay="Top" >
                       <ParentTableRelation>
                           <telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" />
                       </ParentTableRelation>
                         <Columns>
                           <telerik:GridTemplateColumn>
                           <ItemTemplate>
                               <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
                           </ItemTemplate>
                           </telerik:GridTemplateColumn>
                      </Columns>
                   </telerik:GridTableView>
               </DetailTables>
               <Columns>
                   <telerik:GridBoundColumn  Display="false"  HeaderText="CustomerID"  DataField="CustomerID" UniqueName="CustomerID">
                   </telerik:GridBoundColumn>
                </Columns>
           </MasterTableView>
       </telerik:RadGrid>

C#:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "GridTableView1")
    {
        GridDataItem item = (GridDataItem)e.Item;
        GridTableView tableView = (GridTableView)item.NamingContainer;
        int masterIndex = (tableView.ParentItem as GridDataItem).ItemIndex;
        LinkButton btn = (LinkButton)item.FindControl("LinkButton1");
        btn.Attributes.Add("onclick", "ClientClick(this,'" + item.ItemIndex + "','" + masterIndex + "');");
    }
}

Java Script:

<script type="text/javascript">
function ClientClick(obj, index, masterIndex)
{
  var grid = $find("<%=RadGrid1.ClientID %>");
  var MasterTable = grid.get_masterTableView();
  var row = MasterTable.get_dataItems()[masterIndex];
  var maasterTableColvalue= row.get_cell("CustomerID").innerText; //getting parent row cell value
}
</script>

Thanks,
Princy.

0
Mario
Top achievements
Rank 1
answered on 28 Mar 2012, 08:43 PM
Thank you. i will try this tomorrow.

Reiner
Tags
Grid
Asked by
Mario
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mario
Top achievements
Rank 1
Share this question
or