C# 从数据库中填充的排序列表框

C# 从数据库中填充的排序列表框,c#,C#,好的,我用这段代码从一个组合框选择填充一个checkedlistbox。我需要对checkedlistbox和combobox进行排序。现在,我在整理它们时遇到了一个问题。我原以为在数据库中对它们重新排序就可以了,但它们的出现是基于创建时间,而不是其他任何因素 上面这个填充了组合框,我需要它按字母顺序排序 private void cmbDatabaseFill() { string conStr = "Provider=Microsoft.Jet.OLEDB.4

好的,我用这段代码从一个组合框选择填充一个checkedlistbox。我需要对checkedlistbox和combobox进行排序。现在,我在整理它们时遇到了一个问题。我原以为在数据库中对它们重新排序就可以了,但它们的出现是基于创建时间,而不是其他任何因素

上面这个填充了组合框,我需要它按字母顺序排序

    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();
    }
这是用于checkedlistbox的,我需要根据数据库中的order字段对其进行排序。order列只包含数字,显示内容应该显示的顺序

    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();
    }

现在,正如我所说,一切都正常显示和运行,我从checkedlistbox中获取所需的值,稍后使用DataRowView引用db中的列名,而不是绑定valuemember,因为我处理的是每行多个值。最大的问题是,它不是基于Order列显示的,或者它在db中的显示方式。有没有一种简单的方法来完成这两种不同的排序方法?

在设置.DataSource属性之前,请尝试将这些行添加到它们各自的组合框和复选框中:

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

DataViewSettings[Index]和DataViewSettings[newTableName]引用数据集中的命名数据表


Order DESC表示要排序的列名为Order,DESC显然表示顶部的最大值。根据您的要求删除描述或指定ASC。

这非常有效!!!非常感谢你。这就是这个应用程序在准备就绪之前剩下的全部内容,这非常有效。一点也不会减慢速度。现在他们都被分类为ASC,一切都很顺利。非常感谢。
 this.dviewmanager2.DataViewSettings[newTableName].Sort = "Order DESC";