[英]Sort datagridview by newest c#
我有一個小程序將值存儲在“ datagridview”中。 問題在於新值隱藏在數據網格的中間。 我只是希望將最新上傳的“值”放置在數據頂部。 因此,每次有人上載新值時,都應將其存儲在datagridview的頂部。 謝謝。
https://gyazo.com/0f44611556133d8db60e66e910fd4fa3 https://gyazo.com/81a2996928b5f21f1aec5f20f9861b5d
//Showing history
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT (Hund),(Vem),(Vad),(Datum),(Tid) FROM lexidatabase.dbo.tbl_rastad WHERE username = @Username";
cmd.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = Login.username;
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sa = new SqlDataAdapter(cmd);
sa.Fill(dt);
historik.DataSource = dt;
con.Close();
請嘗試以下操作,這是在代碼中對DGV進行排序的方法,DataGridView中有一個稱為“排序”的方法
this.dataGridView1.Sort(this.dataGridView1.Columns["Name"],ListSortDirection.Ascending);
檢查此Microsoft定義鏈接
如果要在頂部看到最后一個添加項,則需要增加值或創建日期。
如果要按名稱或其他字段列出列表。
SELECT (Hund),(Vem),(Vad),(Datum),(Tid) FROM lexidatabase.dbo.tbl_rastad WHERE username = @Username Order By Hund,Vem,Datum asc
您可以使用asc或desc。
Order By Hund,Vem,Datum asc
Order By Hund asc
Order By Datum asc
Order By Tid asc
希望我能幫忙
您可以嘗試在表中添加新列,例如[Creation Date],[Date Add]等,然后在查詢中可以使用ORDER BY子句。
ORDER BY [Creation Date] DESC
要么
ORDER BY [Date Add] DESC
我從“ snn brn ”那里得到了一個解決方案。
cmd.CommandText = "SELECT (Hund),(Vem),(Vad),(Datum),(Tid)
FROM lexidatabase.dbo.tbl_rastad WHERE username = @Username ORDER BY (Datum) desc,(Tid) desc";
因此,基本上,您必須按datum(date)
和tid(time)
對其進行排序,然后在其末尾添加desc
。 然后,列表將在頂部並按順序添加新操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.