views:

42

answers:

1

Hi guys, I am losing my senses here...

I have ComboBox on the form with one property changed, Sorted = true.

This property is messing up with SelectedValue and I would like to know why.

take a look at the code(you can paste it to the new WinForms project and it will work after adding a combobox):

    private void Form1_Load(object sender, EventArgs e)
    {
        List<ColumnFilter> ColumnFilters = new List<ColumnFilter>();
        ColumnFilters.Add(new ColumnFilter("Ope_OpeID", "Ope_OpeID", ColumnFilterType.DataGridViewHidden));
        ColumnFilters.Add(new ColumnFilter("Ope_Kod", "Ope_Kod", ColumnFilterType.DataGridViewHidden));
        ColumnFilters.Add(new ColumnFilter("Ope_Imie", "Ope_Imie", ColumnFilterType.DataGridViewHidden));
        ColumnFilters.Add(new ColumnFilter("Ope_Nazwisko", "Ope_Nazwisko", ColumnFilterType.DataGridViewHidden));

        comboBox1.DataSource = ColumnFilters;
        comboBox1.DisplayMember = "Description";
        comboBox1.ValueMember = "Expression";
    }

    private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    {
        MessageBox.Show(comboBox1.SelectedValue.ToString());
    }
}
public enum ColumnFilterType
{
    DataGridView = 1,
    DataGridViewHidden = 2,
    DataTable = 3
}

public struct ColumnFilter
{
    private string description;
    private string expression;
    public string Description
    {
        get { return description; }
        set { description = value; }
    }
    public string Expression
    {
        get { return expression; }
        set { expression = value; }
    }
    public readonly ColumnFilterType Type;
    public ColumnFilter(string description, string expression, ColumnFilterType type)
    {
        this.description = description;
        this.expression = expression;
        this.Type = type;
    }
    public override string ToString()
    {
        return description;
    }
}

As you can see I manualy add 4 structs to the List and in SelectionChangeCommited event I want to display current value of a selected struct.

So when I select Ope_OpeID I want to see a value Ope_OpeID, when I select Ope_Imie I want to see value Ope_Imie. In other words value must be equal to the displayed text.

The problem is that when I for example select Ope_Imie then the value is Ope_OpeID! When I select Ope_OpeID the value is Ope_Nazwisko. I have no idea why this works in such way.

The "funny" (not so funny after two hours of life wasted..) thing is that when I add to the list different set of items:

        ColumnFilters.Add(new ColumnFilter("1Ope_OpeID", "1Ope_OpeID", ColumnFilterType.DataGridViewHidden));
        ColumnFilters.Add(new ColumnFilter("2Ope_Kod", "2Ope_Kod", ColumnFilterType.DataGridViewHidden));
        ColumnFilters.Add(new ColumnFilter("3Ope_Imie", "3Ope_Imie", ColumnFilterType.DataGridViewHidden));
        ColumnFilters.Add(new ColumnFilter("4Ope_Nazwisko", "4Ope_Nazwisko", ColumnFilterType.DataGridViewHidden));

It works as suspected. So, something is really wrong with sorting, does it sort only on first character???

Thanks for your time.

+1  A: 

Sorted property does not work on a databound combobox. See the Remarks section here.

One option for you is to make ColumnFilter implement IComparable and then sort the List<> directly.

msergeant