Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / September 2007

Tip: Looking for answers? Try searching our database.

Combobox Text and SelectedIndex problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
KevH - 20 Sep 2007 09:46 GMT
Hi

I am having trouble when I programmatically change the SelectedIndex of a
databound combobox in .NET 1.1.

I am finding that the Text property no longer reflects what is displayed in
the combobox.

I have loaded a Dataset with a list of names ("Name0", "Name1" and "Name2")
from a SQL Server table and bound the DataTable to a combobox in the
Form_Load event.
I've then coded up the SelectedIndexChanged event of the combobox to output
the SelectedIndex and Text properties of the combobox each time the value is
changed.
This all works correctly, i.e. the combobox displays the values "Name0",
"Name1" and "Name2", and selecting each value in the combobox causes the
following output:
0 Name0
1 Name1
2 Name2
However, I've then added a button to the form which resets the value in the
combobox back to the first entry ("Name0") by changing the SelectedIndex
property.

Now, after pressing the button and selecting each entry in the combobox, the
output is as follows:
0 Name0
1 Name1
2 Name0

I.e. When I select "Name2" in the combobox, the SelectedIndex is output
correctly, but the Text is output as "Name0" rather than "Name2".

Is this a known bug (and is there a workaround) or am I doing something wrong?

The code is as follows:

private void Form1_Load(object sender, System.EventArgs e)
{
   DataSet ds = new DataSet();

   //MyNames contains "Name0", "Name1" and "Name2"
   SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select
Name from MyNames", GetConnectionString());
                   
   da.Fill(ds);
   DataTable dt = ds.Tables[0];
   DataTable dt = ds.Tables[0];
   comboBox1.DataSource = dt;           
   comboBox1.DisplayMember = "Name";
   comboBox1.DataBindings.Add("Text",dt,"Name");
}

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
  Console.WriteLine(comboBox1.SelectedIndex + " " + comboBox1.Text);
}

private void button1_Click(object sender, System.EventArgs e)
{
  int index = comboBox1.FindString("Name0");
  comboBox1.SelectedIndex = index;
}

Thanks,
Kevin
Morten Wennevik [C# MVP] - 24 Sep 2007 16:21 GMT
Hi KevH,

The problem is the double databinding against the Text property, effectively overriding eachother.  Changing the SelectedIndex (either by selecting from the list or programmatically) will update the text of the previously selected item with the text of the selected item.  Since changing an item updates the Text property, the Text property calls on its databinding to update the datasource, but the selecteditem does not appear to have changed yet causing it to update the current item instead of the new item.  It then gets notified that the SelectedItem has changed and changes its text to reflect the new item.  To fix this, remove the editing capabilities and the Text binding

//    comboBox1.DataBindings.Add("Text", dt, "Name");
      comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

> Hi
>
[quoted text clipped - 62 lines]
> Thanks,
> Kevin

Signature

Happy coding!
Morten Wennevik [C# MVP]

KevH - 24 Sep 2007 16:50 GMT
Excellent - that's been puzzling me for days. It's all working now!

Thanks for your help

Kevin

> Hi KevH,
>
[quoted text clipped - 69 lines]
> > Thanks,
> > Kevin

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.