我正在使用列表视图来显示访问数据库数据。
我是一个名为Date的列。有超过360天的数据,每个数据都有一个数据
示例日期,date format = yyyyMMdd.
显示升序
20111104
20111105
20111106
20111107
20111108
我希望按日期按降序显示数据
20111108
20111107
20111106
20111105
20111104
我正在使用这些代码在列表视图中显示访问数据。
try
{
con.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
OleDbDataAdapter da = new OleDbDataAdapter("select * from MyTable", con);
da.Fill(dt);
int iRecords = 0;
foreach (DataRow myrow in dt.Rows)
{
ListViewItem lItem = new ListViewItem();
lItem.UseItemStyleForSubItems = false;
lItem = listviewTargets.Items.Insert(iRecords, Date.ToString());
//Note in the above line "Date.ToString" is written
lItem.UseItemStyleForSubItems = false;
// listviewTargets.Items.Add(myrow[2].ToString());
lItem.SubItems.Add(myrow[0].ToString());
lItem.SubItems.Add(myrow[3].ToString());
lItem.SubItems.Add(myrow[14].ToString());
lItem.SubItems.Add(myrow[4].ToString(), Color.White, Color.Green, lItem.Font);
lItem.SubItems.Add(myrow[6].ToString());
lItem.SubItems.Add(myrow[7].ToString());
lItem.SubItems.Add(myrow[8].ToString());
lItem.SubItems.Add(myrow[9].ToString());
iRecords++;
lItem = listviewTargets.Items.Insert(iRecords, "");
lItem.UseItemStyleForSubItems = false;
//iRecords = iRecords - 1;
// lItem.SubItems.Add("");
lItem.SubItems.Add("");
lItem.SubItems.Add("");
lItem.SubItems.Add("");
lItem.SubItems.Add(myrow[5].ToString(), Color.White, Color.Red, lItem.Font);
lItem.SubItems.Add(myrow[10].ToString());
lItem.SubItems.Add(myrow[11].ToString());
lItem.SubItems.Add(myrow[12].ToString());
lItem.SubItems.Add(myrow[13].ToString());
iRecords++;
}
// gv_ViewAllEntries.DataSource = ds.Tables["MyTable"];
con.Close();
}
catch (Exception)
{
con.Close();
}
提前致谢
答案 0 :(得分:2)
OleDbDataAdapter da = new OleDbDataAdapter("select * from MyTable order by [Date] desc", con);