简体   繁体   中英

Sorting listbox filled from a database

Ok, so I'm using this code to fill a checkedlistbox from a combobox selection. I need to sort both the checkedlistbox and the combobox. Now, I'm having an issue sorting them. I thought that re-ordering them in the database would do it, but they are appearing based on time of creation not on anything else.

This top one fills the combobox, I need it to sort by alphabetical order.

    private void cmbDatabaseFill()
    {
        string conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=myDB.mdb";
        try
        {
            myConn = new OleDbConnection(conStr);
            myConn.Open();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show("Error in connection ..." + ex.Message);
            return;
        }

        string sqlStr = "SELECT * FROM Index;";

        dAdapter = new OleDbDataAdapter(sqlStr, myConn);

        dset = new DataSet();

        dAdapter.TableMappings.Add("Table", "Index");

        dAdapter.Fill(dset);

        this.dviewmanager = dset.DefaultViewManager;

        this.cmbIndex.DataSource = this.dviewmanager;

        this.cmbIndex.DisplayMember = "Index.list";

        this.myConn.Close();
    }

This one is for the checkedlistbox, I need it to be sorted based on an "order" field in the database. The "order" column simply contains numbers showing what order the contents are supposed to display.

    private void clbDatabaseFill()
    {
        string newTableName = cmbIndex.Text.Replace(" ", "");

        string conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=myDB.mdb";
        try
        {
            myConn2 = new OleDbConnection(conStr);
            myConn2.Open();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show("Error in connection ..." + ex.Message);
        }

        string sqlStr = "SELECT * FROM " + newTableName + ";";

        dAdapter2 = new OleDbDataAdapter(sqlStr, myConn2);

        dset2 = new DataSet();

        dAdapter2.TableMappings.Add("Table", newTableName);

        try
        {
            dAdapter2.Fill(dset2);
        }
        catch (System.Exception)
        {
            return;
        }

        this.dviewmanager2 = dset2.DefaultViewManager;

        this.clbList.DataSource = this.dviewmanager2;

        this.clbList.DisplayMember = newTableName + ".Name";

        this.myConn2.Close();
    }

Now, like I said, everything displays and functions properly, I'm grabbing the values I need from the checkedlistbox later referencing the column names in the db using DataRowView instead of binding a valuemember, because I was dealing with multiple values per row. The big problem is that its not displaying based on the "Order" column, or how it appears in the db. Is there an easy way to accomplish these 2 different sorting methods?

Try adding these lines to their respective combobox and checklistbox BEFORE setting the .DataSource property:

 this.dviewmanager.DataViewSettings["Index"].Sort = "Order DESC";

and

 this.dviewmanager2.DataViewSettings[newTableName].Sort = "Order DESC";

The DataViewSettings["Index"] and DataViewSettings[newTableName] refer to the named DataTables within your DataSet.

The Order DESC indicates that the column to be ordered by is named Order , and DESC obviously meaning largest values at the top. Remove DESC or specify ASC as per your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM