Selected Index Change

6 posts, 0 answers
  1. ASG
    ASG avatar
    9 posts
    Member since:
    Jul 2012

    Posted 12 Mar 2011 Link to this post

    When populating a dropdownlist control the selected index changes, which triggers the action I have setup for the index changing... however when I initially populate the dropdownlist by setting a datatable as its datasource and defining the display and value members, the radDropDownList.SelectedItem.Value is a DataRowView object.  After it's populated, though, the radDropDownList.SelectedItem.Value is an integer as it should be.  Can this be explained to me why this is happening and the best way to work with it?

    My code currently is this, and it will only work when I initially set the datasource:
    private void someOtherMethod ()
    {
           sysParentDropDown.DataSource = dt;
           sysParentDropDown.DisplayMember = "name";
           sysParentDropDown.ValueMember = "panelid";
     
           sysParentDropDown.SelectedIndex = -1;
     
    }
     
     
    private void sysParentDropDown_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
            {
                if (this.sysParentDropDown.SelectedIndex > -1)
                {
                    sysDeviceDropDown.Enabled = true;
                    try
                    {
                        sysDeviceDropDown.DataSource = null;
                        sysDeviceDropDown.Text = null;
                        sysInputDropDown.DataSource = null;
                        sysInputDropDown.Text = null;
     
                        DataRowView drv = (DataRowView)this.sysParentDropDown.SelectedItem.Value;
     
                        string s = "(panelid = " + drv.Row["panelid"].ToString() + ") AND (devid <> 0) AND (inputdevid = 0)";
                        DataRow[] foundRows = deviceNames.Select(s, "name asc");
     
                        DataTable dt = new DataTable("temp");
                        dt.Columns.Add("name");
                        dt.Columns.Add("devid");
     
                        foreach (DataRow dr in foundRows)
                        {
                            dt.ImportRow(dr);
                        }
     
                        DataRow row = dt.NewRow();
                        row["name"] = "";
                        row["devid"] = " ";
                        dt.Rows.InsertAt(row, 0);
     
                        sysDeviceDropDown.DisplayMember = "name";
                        sysDeviceDropDown.ValueMember = "devid";
                        sysDeviceDropDown.DataSource = dt;
     
     
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    sysDeviceDropDown.Enabled = false;
                    sysDeviceDropDown.SelectedText = null;
                    sysInputDropDown.Enabled = false;
                    sysInputDropDown.SelectedText = null;
                }
            }

    Thanks for your time
  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 16 Mar 2011 Link to this post

    Hi ck,

    Thank you for writing.

    Assigning DataSource to RadDropDownList causes SelectedIndexChanged to fire, because the Currency manager changes the current position and this is a normal behavior.
    In case that you do not want this, you should put your code between BeginUpdate and EndUpdate methods, for example:

    sysParentDropDown.BeginUpdate();
    sysParentDropDown.DataSource = dt;
    sysParentDropDown.DisplayMember = "name";
    sysParentDropDown.ValueMember = "panelid";
    sysParentDropDown.SelectedIndex = -1;
    sysParentDropDown.EndUpdate();

    I hope this helps you.

    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!
  3. UI for WinForms is Visual Studio 2017 Ready
  4. ASG
    ASG avatar
    9 posts
    Member since:
    Jul 2012

    Posted 16 Mar 2011 Link to this post

    Thank you Peter - I'm alright with the selected index changing, however I'm a little curious as to why the first time it changes the index the value stored in this.sysParentDropDown.SelectedItem.Value is a DataRowView and then the next time the index changes the value is an integer as it should be?
  5. ASG
    ASG avatar
    9 posts
    Member since:
    Jul 2012

    Posted 16 Mar 2011 Link to this post

    So using the BeginUpdate method, I end up with the dropdownlist having its first item be "System.Data.DataRowView" now... all items under it are correct, but I guess I'm not understanding why I keep getting this DataRowView?
  6. ASG
    ASG avatar
    9 posts
    Member since:
    Jul 2012

    Posted 17 Mar 2011 Link to this post

    I ended up just adding a blank row as the first row in the datatable I'm using and then selected index 1 and it populates the first real value in the list.  Not the most efficient way to do it, but it does the trick.
  7. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 22 Mar 2011 Link to this post

    Hello ck,

    Thank you for the clarification.

    As I mentioned, the process of assigning the DataSource to RadDropDownList causes SelectedIndexChanged to fire. So, this line of code:

    sysParentDropDown.DataSource = dt;
    will fire SelectedIndex change - at this moment you do not have a ValueMember set and the value stored in this.sysParentDropDown.SelectedItem.Value will be a DataRowView.

    You should set the ValueMember/DisplayMember before setting the DataSource:
    sysParentDropDown.BeginUpdate();
    sysParentDropDown.DisplayMember = "name";
    sysParentDropDown.ValueMember = "panelid";
    sysParentDropDown.DataSource = dt;
    sysParentDropDown.SelectedIndex = -1;
    sysParentDropDown.EndUpdate();

    I hope this helps.

    Best wishes,
    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
UI for WinForms is Visual Studio 2017 Ready