7 Replies - 393 Views - Last Post: 10 September 2012 - 09:32 PM Rate Topic: -----

#1 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 17-October 10

Combobox does not pick up user selectedindex correctly

Posted 05 September 2012 - 06:29 PM

I have problem with this. Everytime user select any items in the combobox the result always points to the first item.

combobox is binded to a table upon result of a query

private void frmTaskAdd_Load(object sender, EventArgs e)
{
...
...
...
daTaskCombo = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM taskdescription WHERE shoretype = '" + result + "'", con);

dsTaskDescription = new DataSet("Task Description");
daTaskCombo.Fill(dsTaskDescription, "Task Desc Combo");

cmbActivityType.DataSource = dsTaskDescription.Tables["Task Desc Combo"];
cmbActivityType.DisplayMember = "description";

}



this is the SelectedIndexChanged event

private void cmbActivityType_SelectionchangeCommitted(object sender, EventArgs e)
{

strActivityType = cmbActivityType.SelectedItem.ToString();

string query = "description='" + strActivityType + "' AND shoretype='" + result + "'";

daTaskDescription = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM taskdescription", con);
daTaskDescription.Fill(dsTaskDescription, "TaskDescription");
DataRow[] drGetIndeksForActivityType = dsTaskDescription.Tables["TaskDescription"].Select(query);

foreach (DataRow drActivity in drGetIndeksForActivityType)
{
indeksActivityType = (int)drActivity["id"];        
}            
}



Please help. i have done debugging myself. tried some advices from forums thread but still failed.

Is This A Good Question/Topic? 0
  • +

Replies To: Combobox does not pick up user selectedindex correctly

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1896
  • View blog
  • Posts: 5,687
  • Joined: 05-May 12

Re: Combobox does not pick up user selectedindex correctly

Posted 05 September 2012 - 06:44 PM

So when you set a breakpoint on:
strActivityType = cmbActivityType.SelectedItem.ToString();



The value of strActivityType is always the same first item?

What is the value of the variable?

This post has been edited by Skydiver: 05 September 2012 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1896
  • View blog
  • Posts: 5,687
  • Joined: 05-May 12

Re: Combobox does not pick up user selectedindex correctly

Posted 05 September 2012 - 07:07 PM

As a quick note, unless the DataRow does something special with ToString(), your call to SelectedItem.ToString() will probably just always result in in a fixed string that is equivalent to DataRow.GetType().ToString().

You should actually cast the SelectedItem to something more usable and get what you need from it. See quick and dirty code below.

    public class FormX : Form
    {
        ComboBox _cmbName = new ComboBox();
        Label _lblDescription = new Label();

        public FormX()
        {
            var list = new List<Tour>();
            list.Add(new Tour("Black Death", "Most of Europe"));
            list.Add(new Tour("Mission Accomplished", "Just Baghdad"));
            list.Add(new Tour("Hamilton", "All your vertices are belong to us."));

            _cmbName.AutoSize = true;
            _cmbName.Location = new Point(10, 10);
            _cmbName.DataSource = list;
            _cmbName.DisplayMember = "Name";
            _cmbName.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);

            _lblDescription.AutoSize = true;
            _lblDescription.Location = new Point(10, 40);

            Controls.Add(_cmbName);
            Controls.Add(_lblDescription);
        }

        void cmb_SelectedIndexChanged(object sender, EventArgs e)
        {
            Tour tour = _cmbName.SelectedItem as Tour;
            _lblDescription.Text = String.Format("{0}: {1}", _cmbName.SelectedIndex, tour.Description);
        }

    }

    class Tour
    {
        public string Name { get; set; }
        public string Description { get; set; }

        public Tour(string name, string description)
        {
            Name = name;
            Description = description;
        }
    }


Was This Post Helpful? 0
  • +
  • -

#4 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 17-October 10

Re: Combobox does not pick up user selectedindex correctly

Posted 05 September 2012 - 09:30 PM

View PostSkydiver, on 05 September 2012 - 06:44 PM, said:

So when you set a breakpoint on:
strActivityType = cmbActivityType.SelectedItem.ToString();



The value of strActivityType is always the same first item?

What is the value of the variable?


So sorry initially the code was like this
strActivityType = cmbActivityType.SelectedText;


Just playing around with all available properties to get my desired result.

It should be string type but always point to the first item description.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1896
  • View blog
  • Posts: 5,687
  • Joined: 05-May 12

Re: Combobox does not pick up user selectedindex correctly

Posted 05 September 2012 - 10:55 PM

What is the style of your ComboBox? Is it set to DropDownList? If so, I believe that the ComboBox never updates SelectedText.
Was This Post Helpful? 1
  • +
  • -

#6 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 17-October 10

Re: Combobox does not pick up user selectedindex correctly

Posted 05 September 2012 - 11:32 PM

View PostSkydiver, on 05 September 2012 - 10:55 PM, said:

What is the style of your ComboBox? Is it set to DropDownList? If so, I believe that the ComboBox never updates SelectedText.


the type is just DropDown.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1896
  • View blog
  • Posts: 5,687
  • Joined: 05-May 12

Re: Combobox does not pick up user selectedindex correctly

Posted 06 September 2012 - 12:37 AM

Please see this paragraph from the MSDN documentation:

Quote

When the user selects an item from the drop-down list or by using the UP ARROW and DOWN ARROW keys, the text for the new item is automatically selected. However, if you try to get the SelectedText value in a SelectedIndexChanged or SelectedValueChanged event handler, the property returns an empty string. This is because, at the time of these events, the previous SelectedText value has been cleared and the new value has not yet been set. To retrieve the current value in a SelectedIndexChanged or SelectedValueChanged event handler, use the SelectedItem property instead.

http://msdn.microsof...lectedtext.aspx
Was This Post Helpful? 2
  • +
  • -

#8 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 17-October 10

Re: Combobox does not pick up user selectedindex correctly

Posted 10 September 2012 - 09:32 PM

View PostSkydiver, on 06 September 2012 - 12:37 AM, said:

Please see this paragraph from the MSDN documentation:

Quote

When the user selects an item from the drop-down list or by using the UP ARROW and DOWN ARROW keys, the text for the new item is automatically selected. However, if you try to get the SelectedText value in a SelectedIndexChanged or SelectedValueChanged event handler, the property returns an empty string. This is because, at the time of these events, the previous SelectedText value has been cleared and the new value has not yet been set. To retrieve the current value in a SelectedIndexChanged or SelectedValueChanged event handler, use the SelectedItem property instead.

http://msdn.microsof...lectedtext.aspx


got it...what i did was, I used back the dataset used to pump in data to the combobox. Instead of using the SelectedChangeCommitted I put the code in the SelectedIndexChanged. Get the Index selected and drill thru the rows and get the ID for the selected rows.

I realized now... to get this is not just really a straight forward task.

thanks for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1