对数据表进行排序

时间:2013-12-03 12:23:00

标签: asp.net vb.net sorting listbox

我的页面上有一个asp.net列表框,我想要排序。

例如:listbox(lstChooseFields)

我的VB代码:

        Private Sub LoadColumns()
        If drpScreen.SelectedIndex > -1 Then
        Dim daLookup As New LookupTableAdapters.LookupTableAdapter
        Dim dtLookup As New System.Data.DataTable

        dtLookup = daLookup.GetNestedControlValues("EM", "ExcelExport.aspx", "drpScreen", "drpScreen", drpScreen.SelectedValue)
        lstChooseFields.DataSource = dtLookup
        lstChooseFields.DataValueField = "LKP_ControlValue"
        lstChooseFields.DataTextField = "LKP_ControlText"
        lstChooseFields.DataBind()

        'Dispose

        daLookup.Dispose()

    End If
End Sub

我做了一些搜索,发现了类似'dtLookup.DefaultView.Sort =“LKP_ControlText”的东西,我可以使用但是它不起作用。我不想只在这个页面上对我的数据库进行排序,这个列表框(lstChooseFields)。

2 个答案:

答案 0 :(得分:0)

使用 .sorted 属性作为列表框,如here所示。

lstChooseFields.Sorted = True 

更新:您的方法是正确的,但只需这样调整即可。我想你错过了 DESC 部分。第二个表达式告诉它如何对数据视图进行排序。

Dim dataView As New DataView(table)
dataView.Sort = " column_name DESC" //DESC or ASC as per requirement
Dim dataTable AS DataTable = dataView.ToTable()

否则试试这个。 排序后一个默认视图,通常你必须通过它回送

foreach(DataRowView r in table.DefaultView)
{
    //... here you get the rows in sorted order
    //insert the listbox items one by one here using listbox.add or listbox.insert  commands
}

答案 1 :(得分:0)

因此,如果你想对数据表进行排序,那么你已经尝试过dtLookup.DefaultView.Sort = "LKP_ControlText"
但你错过了一个单词dtLookup.DefaultView.Sort = "LKP_ControlText asc"。如果LKP_ControlText是数据表中的列名

,这通常有效

您必须在那里使用asc这个词。我在你的问题中找不到。

获取dataTable后,如果需要,可以使用

将相同的结构复制到另一个数据表
Dim dtDuplicateLookup As New DataTable()
dtDuplicateLookup = dtLookup.DefaultView.ToTable(True)

然后访问dtDuplicateLookup以供进一步使用,如果你想保留原来的数据表。