从数据库填充的排序列表框
本文关键字:列表 排序 数据库 填充 | 更新日期: 2023-09-27 17:50:18
好的,所以我使用这段代码从一个组合框选择中填充一个checklistbox。我需要对复选列表框和组合框进行排序。现在,我有一个问题,排序它们。我以为在数据库中重新排序它们就可以了,但是它们是根据创建时间出现的,而不是根据其他任何东西。
上面的这个填充了组合框,我需要它按字母顺序排序。
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();
}
这是为checklistbox,我需要它是基于数据库中的"顺序"字段排序。"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();
}
现在,就像我说的,一切都显示和功能正常,我从checklistbox中抓取我需要的值,稍后使用DataRowView引用数据库中的列名,而不是绑定valuemmember,因为我正在处理每行多个值。最大的问题是,它不显示基于"订单"列,或如何出现在数据库。是否有一种简单的方法来完成这两种不同的分类方法?
尝试在设置.DataSource
属性之前将这些行添加到各自的组合框和复选框中:
this.dviewmanager.DataViewSettings["Index"].Sort = "Order DESC";
和
this.dviewmanager2.DataViewSettings[newTableName].Sort = "Order DESC";
DataViewSettings["Index"]
和DataViewSettings[newTableName]
指的是数据集中命名的数据表。
Order DESC
表示要排序的列命名为Order
, DESC显然表示顶部的最大值。删除DESC或根据您的要求指定ASC