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

Selected Index Change

5 Answers 168 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
ASG
Top achievements
Rank 1
ASG asked on 13 Mar 2011, 04:47 AM
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

5 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 16 Mar 2011, 09:20 AM
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!
0
ASG
Top achievements
Rank 1
answered on 16 Mar 2011, 11:11 PM
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?
0
ASG
Top achievements
Rank 1
answered on 16 Mar 2011, 11:20 PM
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?
0
ASG
Top achievements
Rank 1
answered on 17 Mar 2011, 06:30 PM
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.
0
Peter
Telerik team
answered on 22 Mar 2011, 12:55 PM
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!
Tags
DropDownList
Asked by
ASG
Top achievements
Rank 1
Answers by
Peter
Telerik team
ASG
Top achievements
Rank 1
Share this question
or