简体   繁体   English

按最新的C#排序datagridview

[英]Sort datagridview by newest c#

I have a small program to store the values in a "datagridview". 我有一个小程序将值存储在“ datagridview”中。 The problem is that the new value is hidden in the middle of the datagrid. 问题在于新值隐藏在数据网格的中间。 I simply want the most recent "value" uploaded to be placed at the top of the data. 我只是希望将最新上传的“值”放置在数据顶部。 So everytime someone uploads a new value, it should be stored in the top of the datagridview. 因此,每次有人上载新值时,都应将其存储在datagridview的顶部。 Thanks. 谢谢。

https://gyazo.com/0f44611556133d8db60e66e910fd4fa3 https://gyazo.com/81a2996928b5f21f1aec5f20f9861b5d 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();

Please Try below,This is the way to sort a DGV in code, there is a methord called "Sort" in DataGridView 请尝试以下操作,这是在代码中对DGV进行排序的方法,DataGridView中有一个称为“排序”的方法

this.dataGridView1.Sort(this.dataGridView1.Columns["Name"],ListSortDirection.Ascending);

Check this microsoft Definition link 检查此Microsoft定义链接

If you want to see the last addition at the top, you need an increased value or date of creation. 如果要在顶部看到最后一个添加项,则需要增加值或创建日期。

If you want to list the list by name or another field. 如果要按名称或其他字段列出列表。

SELECT (Hund),(Vem),(Vad),(Datum),(Tid) FROM lexidatabase.dbo.tbl_rastad WHERE username = @Username Order By Hund,Vem,Datum asc

asc or desc you can use. 您可以使用asc或desc。

Order By Hund,Vem,Datum asc

Order By Hund asc

Order By Datum asc

Order By Tid asc

ı hope i cold help 希望我能帮忙

You can try to add new column in your table, like [Creation Date], [Date Add] or something like that, then in your query, you can use ORDER BY clause. 您可以尝试在表中添加新列,例如[Creation Date],[Date Add]等,然后在查询中可以使用ORDER BY子句。

ORDER BY [Creation Date] DESC

or 要么

ORDER BY [Date Add] DESC

I got a solution from "snn brn " which is. 我从“ snn brn ”那里得到了一个解决方案。

cmd.CommandText = "SELECT (Hund),(Vem),(Vad),(Datum),(Tid) 
FROM lexidatabase.dbo.tbl_rastad WHERE username = @Username ORDER BY (Datum) desc,(Tid) desc";

So bascially you have to order it by the datum(date) and tid(time) and then add desc at the end of it. 因此,基本上,您必须按datum(date)tid(time)对其进行排序,然后在其末尾添加desc Then the list will add the new actions on the top, and also in order. 然后,列表将在顶部并按顺序添加新操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM