How to sort data using DataGridView in VB.NET?

泄露秘密 提交于 2019-12-25 02:46:55

问题


I do not have any idea on how to sort data using datagridview in VB.NET. How do I do this by making use of Textbox to input my query, I'm currently using OLEDB. Here is a picture of what I am trying to do.


回答1:


Homework?

You cannot exactly sort data using a DataGridView, but you can set the display order of data in a DataGridView.

Set the SortedColumn property of your DataGridView object to the DataGridViewColumn by which you want your data sorted. If you need a more complex sorting order, you might want to call the Sort method with a custom IComparer. Refer to the MSDN documentation for details.

Or do you actually want to filter your data by the two criteria for which there is an input line in your screenshot? In that case, I'm not exactly sure about the best solution. Probably you need to iterate through all DataGridViewRows and set each row's visibility depending on the entered criteria:

For Each row As DataGridViewRow in dgv.Rows
    row.Visible = {some condition}
Next



回答2:


try following code:

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



回答3:


Does the sort need to be dynamic? That is, does your customer need to be able to click on a header row and sort it?

If not, why not order the query coming back and then bind that object to the grid?

This would not be a good idea if the sort is dynamic because the cost of back and forth to the DB would not be worth it.




回答4:


'declare this first:
imports system.componentmodel


'then put this code into a button or something
DGV.Sort(DGV.Columns(0), ListSortDirection.Ascending)

'DGv = datagridview

goodluck



来源:https://stackoverflow.com/questions/1993447/how-to-sort-data-using-datagridview-in-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!