locked
binding source with two different sort criteria? RRS feed

  • Question

  • Hi!

    I've got a data source with one table, two columns.

    Now, I want two comboboxes displaying them in tandem, each combobox one column. This I got working too.

    The problem is, that the values appear only sorted in one combobox, the one displaying the column the bindingsource uses for sorting.

    So, if I have three rows (a,3), (b,2), (c,1) in my data table, set the binding source to sort according to the letter column, the first combobox shows the letters sorted all right, but the second combobox shows 3,2,1.

    Is it possible to have two comboboxes, one showing a,b,c and the other chowing 1,2,3 and the two comboboxes still synchronized to the same dataset?

    Lots of Greetings!


    Volker

    Friday, April 13, 2012 5:42 PM

Answers

  • Try using one BindingSource object for each ComboBox control and set each BindingSource.DataSource property to your DataTable as a data view using DataTable.AsDataView() extension method. Then specify the Sort property for each BindingSource

    DataTable dt = new DataTable();
    dt.Columns.Add("Col1", typeof(string));
    dt.Columns.Add("Col2", typeof(int));
    
    dt.Rows.Add("a", 3);
    dt.Rows.Add("b", 2);
    dt.Rows.Add("c", 1);
    
    BindingSource bs1 = new BindingSource();
    bs1.DataSource = dt.AsDataView();
    bs1.Sort = "Col1 ASC";
    
    BindingSource bs2 = new BindingSource();
    bs2.DataSource = dt.AsDataView();
    bs2.Sort = "Col2 ASC";
                
    comboBox1.DataSource = bs1;
    comboBox1.DisplayMember = "Col1";
    
    comboBox2.DataSource = bs2;
    comboBox2.DisplayMember = "Col2";


    There is no reasonable excuse for doing anything less than your best. May the source be with you!

    Saturday, April 14, 2012 6:38 AM

All replies

  • Try using one BindingSource object for each ComboBox control and set each BindingSource.DataSource property to your DataTable as a data view using DataTable.AsDataView() extension method. Then specify the Sort property for each BindingSource

    DataTable dt = new DataTable();
    dt.Columns.Add("Col1", typeof(string));
    dt.Columns.Add("Col2", typeof(int));
    
    dt.Rows.Add("a", 3);
    dt.Rows.Add("b", 2);
    dt.Rows.Add("c", 1);
    
    BindingSource bs1 = new BindingSource();
    bs1.DataSource = dt.AsDataView();
    bs1.Sort = "Col1 ASC";
    
    BindingSource bs2 = new BindingSource();
    bs2.DataSource = dt.AsDataView();
    bs2.Sort = "Col2 ASC";
                
    comboBox1.DataSource = bs1;
    comboBox1.DisplayMember = "Col1";
    
    comboBox2.DataSource = bs2;
    comboBox2.DisplayMember = "Col2";


    There is no reasonable excuse for doing anything less than your best. May the source be with you!

    Saturday, April 14, 2012 6:38 AM
  • Will try that and see how it works.

    Thanks a lot!


    Volker

    Monday, April 16, 2012 3:45 PM