Radgrid change GridDropDownColumn's datasource based on selection of another GridDropDownColumn in edit mode

6 posts, 0 answers
  1. Rui
    Rui avatar
    14 posts
    Member since:
    Nov 2012

    Posted 21 Aug 2013 Link to this post

    Hi,
    I have three GridDropDownColumns in my Radgrid:
          <telerik:GridDropDownColumn UniqueName="AddressCountry" ListDataMember="AddressCountry" ColumnEditorID="AddressCountryColumnEditor" HeaderStyle-Width="200"
                                    SortExpression="Id" ListTextField="Name"
                                    ListValueField ="Id" HeaderText="Country" DataField="CountryId" />
     
                         
                                <telerik:GridDropDownColumn UniqueName="AddressProvinceState" ListDataMember="AddressProvinceState" ColumnEditorID="AddressProvinceStateColumnEditor" HeaderStyle-Width="200"
                                    SortExpression="Id" ListTextField="Name"
                                    ListValueField ="Id" HeaderText="Province/State" DataField="ProvinceStateId" />
     
                         
                                <telerik:GridDropDownColumn UniqueName="AddressCity" ListDataMember="AddressCity" ColumnEditorID="AddressCityColumnEditor" HeaderStyle-Width="200"
                                    SortExpression="Id" ListTextField="Name"
                                    ListValueField ="Id" HeaderText="City" DataField="CityId" />


    What I want to do is when in edit mode, when the selection of Country or ProvinceState dropdown change, the datasource of the sub dropdowns change. What I tried is this:
    protected void grid_GridItemCreated(object sender, GridItemEventArgs e)
        {
            if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
            {
     
                GridEditableItem edititem = (GridEditableItem)e.Item;
                int LocationId = Convert.ToInt32(edititem.GetDataKeyValue("LocationId"));
                DataSet dsAddress = somefunction(LocationId);
     
                list1.DataSource = dsAddress.Tables["AddressProvinceState"];
                list1.DataTextField = "Name";
                list1.DataValueField = "Id";
                list1.DataBind();
                RadComboBox list2 = (RadComboBox)edititem["AddressCity"].Controls[0];
                list2.DataSource = dsAddress.Tables["AddressCity"];
                list2.DataTextField = "Name";
                list2.DataValueField = "Id";
                list2.DataBind();
     
                list.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(list_SelectedIndexChanged);
                list.AutoPostBack = true;
     
                list1.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(list1_SelectedIndexChanged);
                list1.AutoPostBack = true;
            }
        }
     
    private void list_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
     
            RadComboBox list = (RadComboBox)sender;
            GridDataItem editItem = (GridDataItem)list.NamingContainer;
            GridEditManager editMan = editItem.EditManager;
            GridDropDownListColumnEditor editor1 = editMan.GetColumnEditor("AddressProvinceState") as GridDropDownListColumnEditor;
            GridDropDownListColumnEditor editor2 = editMan.GetColumnEditor("AddressCity") as GridDropDownListColumnEditor;
     
             
     
     
            DataTable dtProvinceState = ProvinceForCountry(Convert.ToInt32(list.SelectedValue));
            DataTable dtCity = CityForProvince(Convert.ToInt32(dtProvinceState.Rows[0]["Id"]));
     
            editor1.DataSource = dtProvinceState;
            editor1.DataTextField = "Name";
            editor1.DataValueField = "Id";
            editor1.DataBind();
            editor2.DataSource = dtCity;
            editor2.DataTextField = "Name";
            editor2.DataValueField = "Id";
            editor2.DataBind();
     
        }
     
     
        private void list1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
     
            RadComboBox list = (RadComboBox)sender;
            GridEditFormItem editItem = (GridEditFormItem)list.NamingContainer;
            GridEditManager editMan = editItem.EditManager;
            GridDropDownListColumnEditor editor = editMan.GetColumnEditor("AddressCity") as GridDropDownListColumnEditor;
     
             
     
            DataTable dtCity = CityForProvince (Convert.ToInt32(list.SelectedValue));
            editor.DataSource = dtCity;
            editor.DataTextField = "Name";
            editor.DataValueField = "Id";
            editor.DataBind();
     
        }
    the tables are retrieved correctly, but when I do databind for the dropdown, it doesn't bind with the new tables, still bind with the old ones.
    Can I get some help for this?
    Thanks
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Aug 2013 Link to this post

    Hi Rui,

    Im not sure about your requirements.Below is a code snippet on changing one RadcomboBox value when another RadComboBox value changes,using GridTemplateColumn.Here the DatasourceId for the RadComboBox is changed on the SelectedIndexChanged event.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" OnItemCreated="RadGrid1_ItemCreated" AutoGenerateEditColumn="true">
        <MasterTableView DataKeyNames="OrderID">
            <Columns>         
                <telerik:GridTemplateColumn HeaderText="OrderID">
                    <ItemTemplate>
                        <%# Eval("OrderID")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadComboBox ID="RadCombobox1" runat="server">
                        </telerik:RadComboBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="OrderID">
                    <ItemTemplate>
                        <%# Eval("OrderID")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadComboBox ID="RadCombobox2" runat="server">
                        </telerik:RadComboBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>      
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
        {
            GridEditableItem edititem = (GridEditableItem)e.Item; 
           
            RadComboBox list = (RadComboBox)edititem.FindControl("RadCombobox1");     
            list.DataSourceID = "SqlDataSource1";
            list.DataTextField = "OrderID";
            list.DataValueField = "OrderID";
            list.DataBind();
     
            RadComboBox list1 = (RadComboBox)edititem.FindControl("RadCombobox2"); 
            list1.DataSourceID = "SqlDataSource1";
            list1.DataTextField = "OrderID";
            list1.DataValueField = "OrderID";
            list1.DataBind();
       
            list.AutoPostBack = true;
            list.SelectedIndexChanged +=new RadComboBoxSelectedIndexChangedEventHandler(list_SelectedIndexChanged);           
        }
    }
    private void list_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RadComboBox list = (RadComboBox)sender;
        GridEditableItem editItem = (GridEditableItem)list.NamingContainer;
        RadComboBox list1 = (RadComboBox)editItem.FindControl("RadCombobox2");
        list1.DataSourceID = "SqlDataSource2"//Changing the DataSource
        list1.DataTextField = "OrderID";
        list1.DataValueField = "ShipCity";
        list1.DataBind();
    }

    Thanks,
    Princy

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rui
    Rui avatar
    14 posts
    Member since:
    Nov 2012

    Posted 22 Aug 2013 Link to this post

    Hi Princy,

    Thanks, the datasource does the binding now, guess it just doesn't work with GridDropDownColumn.
    But, how do I bind the selected value to my table  when I do update in my radgrid? 
    And also, the radgrid is binding those columns by the CountryId, ProvinceId and CityId. But I want to display the "name" in view mode. The "name" is a column in the datasource table of the combobox in edit mode. How should I display that in view mode?
    Thanks for the help
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Aug 2013 Link to this post

    Hi Rui,

    To display the "name" in the view mode,please try the following code snippet.

    ASPX:
    <telerik:GridTemplateColumn>
    <ItemTemplate>
       <asp:Label ID="Label1" runat="server" ></asp:Label>               
     </ItemTemplate>
     <EditItemTemplate>
       <telerik:RadComboBox ID="RadCombobox1" runat="server">
       </telerik:RadComboBox>
     </EditItemTemplate>
    </telerik:GridTemplateColumn>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridDataItem)
           {
               GridDataItem data = (GridDataItem)e.Item;
               Label labeltemplate = (Label)data.FindControl("Label1"); //Access the label in the template column in view mode
               // query to get the "name" column from datasource
               labeltemplate.Text = "name"; //set it to label text
           }
       }

    Thanks,
    Princy
  6. Jesús
    Jesús avatar
    53 posts
    Member since:
    Oct 2007

    Posted 28 Jun Link to this post

    And what have I do with the same scenario, but in batch edit mode?
  7. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 01 Jul Link to this post

    Hi,

    Using cascading dropdown controls is not available for Batch edit mode out of the box. However, there is a code-library that illustrates how you can implement the behavior.

    Note that the code-library illustrates a custom scenario. You should test it to see how it works for you and apply adjustments if necessary.



    Regards,
    Viktor Tachev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017