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

Setting default value on GridDropDownColumn to parent record ID

1 Answer 147 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 2
Jonathan asked on 02 Nov 2009, 11:51 PM
having trouble setting the value of a GridDropDownColumn in a RadGrid detail table. Following the code at:

http://www.telerik.com/help/aspnet-ajax/grdcustomizeconfiguregriddropdowncolumn.html

my source code is as follows:

ASPX

<%@ Page Title="" Language="C#" MasterPageFile="~/admin.Master" AutoEventWireup="true" 
    CodeBehind="regions2.aspx.cs" Inherits="CRD.EAUtilitiesAdmin.regions2" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"
    <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" 
        AutoGenerateColumns="False" DataSourceID="ldsRegions" GridLines="None" OnItemDataBound="RadGrid1_ItemDataBound" 
        Skin="Office2007"
        <MasterTableView DataKeyNames="intRegionID" DataSourceID="ldsRegions" AllowAutomaticDeletes="True" 
            AllowAutomaticInserts="True" AllowAutomaticUpdates="True" CommandItemDisplay="TopAndBottom" 
            EditMode="InPlace" PageSize="20" Name="Regions"
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <Columns> 
                <telerik:GridBoundColumn DataField="intRegionID" DataType="System.Int32" HeaderText="ID" 
                    ReadOnly="True" SortExpression="intRegionID" UniqueName="intRegionID"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="strRegion" HeaderText="Region" SortExpression="strRegion" 
                    UniqueName="strRegion"
                </telerik:GridBoundColumn> 
                <telerik:GridDropDownColumn DataField="intSuperRegionID" DataSourceID="ldsSuperRegions" 
                    HeaderText="Super Region" ListTextField="strSuperRegion" ListValueField="intSuperRegionID" 
                    UniqueName="dd_SuperRegions"
                </telerik:GridDropDownColumn> 
                <telerik:GridEditCommandColumn UniqueName="EditCommand1" EditText="Edit Record" ButtonType="LinkButton"
                </telerik:GridEditCommandColumn> 
                <telerik:GridButtonColumn CommandName="delete" Text="delete record" UniqueName="DeleteColumn1" 
                    ConfirmText="Delete this Record?"
                </telerik:GridButtonColumn> 
            </Columns> 
            <DetailTables> 
                <telerik:GridTableView DataKeyNames="intChargeSources_X_Regions" DataSourceID="ldsRegions_X_ChargeSources" 
                    runat="server" CommandItemDisplay="Bottom" Name="ChargeSources_X_Regions" AllowAutomaticDeletes="True" 
                    AllowAutomaticInserts="True" AllowAutomaticUpdates="True" EditMode="InPlace" 
                    CommandItemSettings-AddNewRecordText="Add child record" Width="100%"
                    <ParentTableRelation> 
                        <telerik:GridRelationFields DetailKeyField="intRegionID" MasterKeyField="intRegionID" /> 
                    </ParentTableRelation> 
                    <Columns> 
                        <telerik:GridDropDownColumn DataSourceID="ldsRegions" UniqueName="dd_Regions" SortExpression="strRegion" 
                            HeaderText="Region" ListTextField="strRegion" ListValueField="intRegionID" DataField="intRegionID"
                        </telerik:GridDropDownColumn> 
                        <telerik:GridDropDownColumn DataSourceID="ldsChargeSources" UniqueName="dd_ChargeSources" 
                            SortExpression="strChargeSource" HeaderText="Charge Source" ListTextField="strChargeSource" 
                            ListValueField="intChargeSourceID" DataField="intChargeSourceID"
                        </telerik:GridDropDownColumn> 
                        <telerik:GridEditCommandColumn UniqueName="EditCommand2" EditText="Edit child record" 
                            ButtonType="LinkButton"
                        </telerik:GridEditCommandColumn> 
                        <telerik:GridButtonColumn CommandName="delete" Text="delete child record" UniqueName="DeleteColumn2" 
                            ConfirmText="Delete this Record?"
                        </telerik:GridButtonColumn> 
                    </Columns> 
                </telerik:GridTableView> 
            </DetailTables> 
        </MasterTableView> 
    </telerik:RadGrid> 
    <asp:LinqDataSource ID="ldsRegions" runat="server" ContextTypeName="CRD.EAUtilitiesAdmin.data.dbEAUtilitiesDataContext" 
        EnableDelete="True" EnableInsert="True" EnableUpdate="True" OrderBy="strRegion" 
        TableName="tblRegions"
    </asp:LinqDataSource> 
    <asp:LinqDataSource ID="ldsSuperRegions" runat="server" ContextTypeName="CRD.EAUtilitiesAdmin.data.dbEAUtilitiesDataContext" 
        OrderBy="strSuperRegion" TableName="tblSuperRegions"
    </asp:LinqDataSource> 
    <asp:LinqDataSource ID="ldsRegions_X_ChargeSources" runat="server" ContextTypeName="CRD.EAUtilitiesAdmin.data.dbEAUtilitiesDataContext" 
        EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="tblChargeSources_X_tblRegions" 
        Where="intRegionID == @intRegionID"
        <WhereParameters> 
            <asp:ControlParameter ControlID="RadGrid1" Name="intRegionID" PropertyName="SelectedValue" 
                Type="Int32" /> 
        </WhereParameters> 
    </asp:LinqDataSource> 
    <asp:LinqDataSource ID="ldsChargeSources" runat="server" ContextTypeName="CRD.EAUtilitiesAdmin.data.dbEAUtilitiesDataContext" 
        OrderBy="strChargeSource" TableName="tblChargeSources"
    </asp:LinqDataSource> 
</asp:Content> 
 

DOT CS
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
 
using Telerik.Web.UI; 
 
namespace CRD.EAUtilitiesAdmin 
    public partial class regions2 : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
 
        } 
 
        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        { 
            // if you are inserting or updating a record in a child table, force the region drop down to display the region of the parent record 
            if ((e.Item is GridEditableItem && ((GridEditableItem)e.Item).IsInEditMode)) 
            { 
                if ((e.Item.OwnerTableView.Name.ToString() == "ChargeSources_X_Regions")) 
                { 
                    GridEditableItem editItem = (GridEditableItem)e.Item; 
                    GridEditManager editMan = editItem.EditManager; 
 
                    GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)editMan.GetColumnEditor("dd_Regions"); 
 
                    //in case you have RadComboBox editor for the GridDropDownColumn (this is the default editor), 
                    //you will need to use ComboBoxControl below instead of DropDownListControl 
                    //Then you can modify the list control as per your custom conventions 
 
                    GridDataItem parentItem = (GridDataItem)e.Item.OwnerTableView.ParentItem; 
 
                    editor.DropDownListControl.SelectedValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["intRegionID"].ToString(); 
 
                    ////tried direct casting the DropDownList (below) but still came back empty 
                    //DropDownList ddList = editor.DropDownListControl; 
                    //ddList.Items.Insert(0, new ListItem("ddd","0")); 
                    //ddList.SelectedIndex = 3
                } 
            } 
        } 
    } 
 

Basically what I want: When user expands a region and clicks on "Add child record" (to add a child record to that region), the dd_Regions drop down column is automatically set to the value of the parent record.

when i debug, and step through the RadGrid1_ItemDataBound method, the parentItem variable is populated correctly, and has the ID that I am expecting. But editor.DropDownListControl comes back empty (Items.Count = 0). direct casting to a DropDownList returns the same (empty count).

Any help appreciated!

thanks in advance

/jonathan


1 Answer, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 05 Nov 2009, 12:22 PM
Hi Jonathan,

Taking into consideration that the intRegionID field is one and the same for a parent record and all of its child rows (given the ParentTableViewRelation you have specified), then you do not need any additional code to make the dropdown display as selected value the intRegionID of the parent item - the DataField property of the drop-down column being set to intRegionID should suffice. This goes for the case when you enter edit mode. When you are putting your grid into insert mode, you need to set a default value for the intRegionID field of the new child table record equal to the value in the same field of the parent item. How to set default values is explained in the following help article:
http://www.telerik.com/help/aspnet-ajax/grdinsertingvaluesinplaceandeditforms.html
under the section: Setting predefined values for different column editors

I hope this helps.

Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 2
Answers by
Tsvetoslav
Telerik team
Share this question
or