简体   繁体   中英

Sorting unbound datagridview programmatically

I need to sort the datagridview programmatically. I googled a lot but nothing worked for me.

Datagridview is not bound to any datasource. Data is being added manually. My requirement is to sort it as when a 'Sort' button is pressed.

Can anyone suggest me code in vb.net?

Try like this

DataGridView1.Columns(0) -> Give which column you want to sort System.ComponentModel.ListSortDirection.Ascending -> Give direction of ascending or decending

 Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As 
 System.EventArgs) Handles Button1.Click

        DataGridView1.Sort(DataGridView1.Columns(0), 
        System.ComponentModel.ListSortDirection.Ascending)

 End Sub

The code above works. You can, of course, choose any valid column. Here it is in C#, the only difference is the square braces []:
gridview.Sort( gridview.Columns[0], System.ComponentModel.ListSortDirection.Ascending );

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.

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