Get value of Details Table in Code Behind

9 posts, 1 answers
  1. cimscims
    cimscims avatar
    14 posts
    Member since:
    Jul 2010

    Posted 20 Jul 2010 Link to this post

    I have the following code. When clicked on the pencil icon, edit form appears. How to get the value of the Details Table Column into the code behind. Which event(UpdateCommand??) should i use in the code behind?


    <
    MasterTableView Width="100%" DataKeyNames="ModId" AllowMultiColumnSorting="True" HeaderStyle-Font-Size="14px" HeaderStyle-Font-Bold="true" >                               
    <DetailTables>
      <telerik:GridTableView DataKeyNames="ModId" AutoGenerateColumns="false" Width="100%" HeaderStyle-Font-Size="12px" HeaderStyle-Font-Bold="true" HeaderStyle-BackColor="#c0c0c0" AlternatingItemStyle-BackColor="#cccccc" >
      <ParentTableRelation>
      <telerik:GridRelationFields DetailKeyField="ModId" MasterKeyField="ModId" />
                                                                                        </ParentTableRelation>                                        
      <Columns>
        <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn1">
        <HeaderStyle Width="20px" />
        <ItemStyle CssClass="MyImageButton" />
        </telerik:GridEditCommandColumn>
        <telerik:GridBoundColumn SortExpression="IssuedDate" HeaderText="Issued Date" readonly="true" DataField="IssuedDate" ItemStyle-Width="200px">
        </telerik:GridBoundColumn>
  2. cimscims
    cimscims avatar
    14 posts
    Member since:
    Jul 2010

    Posted 20 Jul 2010 Link to this post

    Any clues??
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. cimscims
    cimscims avatar
    14 posts
    Member since:
    Jul 2010

    Posted 20 Jul 2010 Link to this post

    telerik admin/mvp's....Please respond.
  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 Jul 2010 Link to this post

    Hello,

    You can try the following code snippet in ItemDataBound event to access the BoundColumn value of DetailTable when it is in edit mode. For that, assign one name for GridTableView and then by using ColumnUniqueName of that BoundColumn access the value.

    ASPX:
    <DetailTables>
        <telerik:GridTableView Name="GridTableView1" rDataKeyNames="ModId" AutoGenerateColumns="false"
            Width="100%" HeaderStyle-Font-Size="12px" HeaderStyle-Font-Bold="true" HeaderStyle-BackColor="#c0c0c0"
            AlternatingItemStyle-BackColor="#cccccc">
            <ParentTableRelation>
                <telerik:GridRelationFields DetailKeyField="ModId" MasterKeyField="ModId" />
            </ParentTableRelation>
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn1">
                    <HeaderStyle Width="20px" />
                    <ItemStyle CssClass="MyImageButton" />
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn SortExpression="IssuedDate" HeaderText="Issued Date" ReadOnly="true"
                    DataField="IssuedDate" ItemStyle-Width="200px" UniqueName="IssuedDate">
                </telerik:GridBoundColumn>
            </Columns>
    </DetailTables>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridEditFormItem && e.Item.IsInEditMode && e.Item.OwnerTableView.Name == "GridTableView1")
           {
               GridEditFormItem editItem = (GridEditFormItem)e.Item;
               TextBox txt = (TextBox)editItem["IssuedDate"].Controls[0];
                string value = txt.Text;  // get the value
                string keyValue = editItem.GetDataKeyValue("ModId").ToString(); // access DataKeyValue of DetailTable
            }
       }

    Also refer the documentation Accessing cells and rows which explains how to access the cells and rows in a grid.

    Thanks,
    Princy.
  6. cimscims
    cimscims avatar
    14 posts
    Member since:
    Jul 2010

    Posted 21 Jul 2010 Link to this post

    Thanks Princy. The code worked. I have a question though. How to come out of the edit mode? I have used e.canceled = true but it is not working.
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Jul 2010 Link to this post

    Hello,

    I am not quite sure about your scenario. I guess you want to close the edit form of DetailTable from code behind. If you know the index of the item, you can directly close the edit form by setting 'e.Item.Edit = false'. Otherwise loop through each item in grid and close the correspoding edit form in DetailTable.

    C#:
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
           {
               item.ChildItem.NestedTableViews[0].ClearEditItems();
               item.ChildItem.NestedTableViews[0].Rebind();
           }

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

    Posted 07 Jan 2015 in reply to Princy Link to this post

    It's unlikely the DetailTable would have the same DataKey as the Parent, so the only solution I can think of is:

    DataKeyNames="[Child Primary Key], [Parent Primary Key]"

    And then just fetch the Parent Primary Key. 
  9. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 07 Jan 2015 in reply to Peter Link to this post

    Actually this doesn't work for me - I'm doing an insert and need the parent's value. How do I get it? 
  10. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 07 Jan 2015 in reply to Peter Link to this post

    This seems to work: 

    case RadGrid.PerformInsertCommandName:

    GridEditableItem newItem = (GridEditableItem)e.Item;
    newItem.OwnerTableView.ParentItem.GetDataKeyValue("VRPExtractScheduleID")
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017