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

Get value of Details Table in Code Behind

8 Answers 404 Views
Grid
This is a migrated thread and some comments may be shown as answers.
cimscims
Top achievements
Rank 1
cimscims asked on 20 Jul 2010, 02:18 PM
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>

8 Answers, 1 is accepted

Sort by
0
cimscims
Top achievements
Rank 1
answered on 20 Jul 2010, 03:23 PM
Any clues??
0
cimscims
Top achievements
Rank 1
answered on 20 Jul 2010, 05:05 PM
telerik admin/mvp's....Please respond.
0
Accepted
Princy
Top achievements
Rank 2
answered on 21 Jul 2010, 06:46 AM
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.
0
cimscims
Top achievements
Rank 1
answered on 21 Jul 2010, 02:13 PM
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.
0
Princy
Top achievements
Rank 2
answered on 22 Jul 2010, 11:45 AM
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.
0
Peter
Top achievements
Rank 1
answered on 08 Jan 2015, 01:10 AM
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. 
0
Peter
Top achievements
Rank 1
answered on 08 Jan 2015, 01:16 AM
Actually this doesn't work for me - I'm doing an insert and need the parent's value. How do I get it? 
0
Peter
Top achievements
Rank 1
answered on 08 Jan 2015, 01:17 AM
This seems to work: 

case RadGrid.PerformInsertCommandName:

GridEditableItem newItem = (GridEditableItem)e.Item;
newItem.OwnerTableView.ParentItem.GetDataKeyValue("VRPExtractScheduleID")
Tags
Grid
Asked by
cimscims
Top achievements
Rank 1
Answers by
cimscims
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Peter
Top achievements
Rank 1
Share this question
or