Databinding Drop down list based on another dropdown list

3 posts, 0 answers
  1. Afraz Ali
    Afraz Ali avatar
    12 posts
    Member since:
    Jul 2009

    Posted 15 Mar 2011 Link to this post

    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. Afraz Ali
    Afraz Ali avatar
    12 posts
    Member since:
    Jul 2009

    Posted 15 Mar 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 17 Mar 2011 Link to this post

    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!
Back to Top