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

Databinding Drop down list based on another dropdown list

2 Answers 56 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Afraz Ali
Top achievements
Rank 1
Afraz Ali asked on 15 Mar 2011, 08:57 AM
Hi guys,
I am getting this strange error and I am really pulling my hair at the moment. My scenario is simple. I have a drop down list for Categories. When the user Selects and category, I populate another drop down list of subcategory based on the selected category.
Here is the code
private void ddlCategory_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {  
            this.PopulateSubCategories();
        }
 
 private void PopulateSubCategories()
        {           
            DataTable dtSubCategories = new SubCategory().GetSubCategoryByCategoryID(Convert.ToInt64(ddlCategory.SelectedItem.Value));
            DataRow dr = dtSubCategories.NewRow();
            dr["ID"] = 0;
            dr["Name"] = "Any";
            dr["CategoryID"] = 0;
            dr["isACtive"] = true;
            dtSubCategories.Rows.InsertAt(dr, 0);
 
            this.ddlSubCategory.DataSource = dtSubCategories;
            this.ddlSubCategory.DisplayMember = "Name";
            this.ddlSubCategory.ValueMember = "ID";
            this.ddlSubCategory.SelectedIndex = 0;
        }

The problem is that ddlCategory.SelectedItem.Value statement throws exception, Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'. If i skip this function first time and change the index to some other than it works fine. Why is it not returning the SelectedValue on first bind
Thanks
Afraz Ali

2 Answers, 1 is accepted

Sort by
0
Afraz Ali
Top achievements
Rank 1
answered on 15 Mar 2011, 10:21 AM
ok here is the update. Turns out that drop down List's "ValueMember" property is set to null when its data source is set for first time. So when i added this check it has started working.

 if (!String.IsNullOrEmpty(ddlCategory.ValueMember))

But I am still confused as to why it shows this behavior and would like someone to explain to me whats going on under the hood.
Thanks
Afraz
0
Peter
Telerik team
answered on 17 Mar 2011, 01:52 PM
Hi Afraz Ali,

Thank you for the writing.

Assigning a DataSource to RadDropDownList for first time fires the SelectedIndexChanged event, because the Currency manager changes the current position and this is a normal behavior - at this moment you do not have a ValueMember set yet. So you should set first ValueMember and DisplayMember to avoid this scenario.

I hope this helps.

Greetings,
Peter
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
DropDownList
Asked by
Afraz Ali
Top achievements
Rank 1
Answers by
Afraz Ali
Top achievements
Rank 1
Peter
Telerik team
Share this question
or