I have a small program to store the values in a "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. Thanks.
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
this.dataGridView1.Sort(this.dataGridView1.Columns["Name"],ListSortDirection.Ascending);
Check this microsoft Definition link
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.
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.
ORDER BY [Creation Date] DESC
or
ORDER BY [Date Add] DESC
I got a solution from "snn brn " which is.
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. Then the list will add the new actions on the top, and also in order.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.