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

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

5 Answers 364 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rui
Top achievements
Rank 1
Rui asked on 21 Aug 2013, 04:04 PM
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

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Aug 2013, 09:58 AM
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

0
Rui
Top achievements
Rank 1
answered on 22 Aug 2013, 02:47 PM
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
0
Princy
Top achievements
Rank 2
answered on 23 Aug 2013, 06:51 AM
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
0
Jesús
Top achievements
Rank 1
answered on 28 Jun 2016, 03:04 PM
And what have I do with the same scenario, but in batch edit mode?
0
Viktor Tachev
Telerik team
answered on 01 Jul 2016, 10:42 AM
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.
Tags
Grid
Asked by
Rui
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Rui
Top achievements
Rank 1
Jesús
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or