Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am Using Datagridview Using, i don't have any database Record.
I am Adding Rows using Textbox into Datagridview.
I want to Show Last Record added in First row...
Please Help me How i can?

What I have tried:

dg1.Sort(dg1.Rows(0), ListSortDirection.Descending)
Updated 4-Nov-22 0:04am
Richard MacCutchan 1-Nov-22 10:43am    
And what is the problem?
Soft Management 1-Nov-22 10:49am    
Not Working
Richard MacCutchan 1-Nov-22 11:06am    
Yes, that really explains everything we need to know doesn't it? If you want people to help you then you need explain exactly what the problem is and where it occurs; there is no way that we can guess.
PIEBALDconsult 1-Nov-22 12:51pm    
Typically you would sort the backing store.
If you have a DataTable as the backing store, use its DefaultView property instead and set its Sort property.
Michael_Davies 1-Nov-22 14:14pm    
Hi, See

1 solution

Two points here - @michael_davies has given you the solution to
I want to Show Last Record added in First row...
Here is his link as a clickable link How to add new row at particular position in datagridview[^]. Essentially, instead of inserting a new row at the bottom of the control, add it as row 0 each time. E.g If drow is your new row
dg1.Rows.InsertAt(drow, 0)
To address the sorting aspect of your question (which is no longer required, but I still think it is worth pointing this out) ...Your code
dg1.Sort(dg1.Rows(0), ListSortDirection.Descending)
is sorting on the first column of the DataGridView. If that column does not contain an index or the time the row was added then you will not get the order you want. You could add another column to your DGV and populate it each time with dg1.Rows.Count + 1. Then sort on that column. If you don't want it to be visible, then just set the Visible property of the column to False
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900