好的,所以我使用此代码从组合框选择填充checkedlistbox。我需要对checkedlistbox和组合框进行排序。现在,我正在对它们进行排序。我认为在数据库中对它们进行重新排序会做到这一点,但它们是基于创建时间而不是其他任何东西出现的。从数据库中填充排序列表框
这个第一个填充组合框,我需要它按字母顺序排序。
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,我需要它根据数据库中的“订单”字段排序。 “订单”栏只包含显示内容应该显示的顺序的数字。现在
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的,而不是绑定valuemember引用在数据库中的列名的值,因为我处理每行有多个值。最大的问题是它不是基于“Order”列显示的,或者它在数据库中的显示方式。有没有简单的方法来完成这两种不同的排序方法?
这工作完美!非常感谢。这是在这个应用程序准备好之前剩下的所有东西,而且这个工作很好。也不会减慢速度。现在他们都被分类为ASC,并且一切都很好。谢谢。 – James 2011-04-17 05:09:37
@詹姆斯:不客气!很高兴有帮助! – 2011-04-17 05:20:45