在sql中使用AS时对DropDownList进行排序

时间:2017-05-18 13:55:34

标签: sql vb.net

我在排序下拉列表时遇到问题。

我尝试了几种不同的方法,我在这里使用数据视图,但无法让它们工作。

这是我的代码,它创建了我希望按[姓氏]排序的下拉列表。

Dim SqlQuery As String = "SELECT [First Name] + ' ' + [Last Name] + ' ' + [Email] + ' ' + [Phone] AS [FullInfo] FROM [Official]"

con.Close()
con.Open()

Dim dt = New DataTable
Using sqlconnecton = New SqlConnection(con.ConnectionString)
    Using da = New SqlDataAdapter(SqlQuery, con)
        da.Fill(dt)
    End Using
End Using

DropDownList1.DataSource = dt
DropDownList1.DataTextField = "FullInfo"
DropDownList1.DataValueField = "FullInfo"
DropDownList1.DataBind()

更新1

我不应该试图简化这个问题。我的查询如何完成比上面发布的要复杂得多。所以实际发生的是这个查询在For Each循环中,每次循环时找到一条记录并将其添加到下拉列表中。由于查询是这样完成的,因此我不能按姓氏排序,因为它一次只有一条记录被添加到下拉列表中。

以下是正在进行的完整代码:

 For Each row As DataRow In table.Rows 'Check each official for availibility and fill drop down list if needed



                    Dim col1 As Int32 = row.Field(Of Int32)(0)
                    Dim col1string As String = col1


                    'now get block info for each person
                    Dim SqlQuery2 As String = "Where ([Organization] = '" + Session("ID") + "' OR [Organization 2] = '" + Session("ID") + "' OR [Organization 3] = '" + Session("ID") + "') AND [ID] = '" + col1string + "'"
                    Dim NumOfBlocks As Integer = row.ItemArray.Count - 10
                    Dim strblocks As String = "Blocks"
                    Dim FirstChange As String = "No"
                    For i = 1 To NumOfBlocks
                        If i = NumOfBlocks Then 'Last one dont end with AND
                            Dim Num As String = i

                            If String.IsNullOrEmpty(row.Field(Of String)(strblocks + Num)) Then 'dont include
                            Else 'include data
                                If FirstChange = "Yes" Then 'We have an AND

                                    SqlQuery2 = SqlQuery2 & "" + strblocks + Num + " <> '" + Session("DateTxt") + "'"

                                Else 'We dont have an AND

                                    SqlQuery2 = SqlQuery2 & " AND " + strblocks + Num + " <> '" + Session("DateTxt") + "'"

                                End If

                            End If
                        Else
                            Dim Num As String = i

                            If String.IsNullOrEmpty(row.Field(Of String)(strblocks + Num)) Then 'dont include

                            Else 'include data
                                FirstChange = "Yes"
                                SqlQuery2 = SqlQuery2 & " AND " + strblocks + Num + " <> '" + Session("DateTxt") + "' And "
                            End If


                        End If

                    Next

                    'Trim String if it ends with just "and" so an error is not thrown in sql below
                    If SqlQuery2.EndsWith("And ") Then
                        SqlQuery2 = SqlQuery2.Substring(0, SqlQuery2.Length - 5)
                    Else

                    End If

                    con.Close()

                    Dim SqlQuery As String = "SELECT [First Name] + ' ' + [Last Name] + ' ' + [Email] + ' ' + [Phone] AS [FullInfo] FROM [Official]"


                    con.Close()
                    con.Open()
                    Dim dt = New DataTable
                    Using sqlconnecton = New SqlConnection(con.ConnectionString)
                        Using da = New SqlDataAdapter(SqlQuery & SqlQuery2, con)

                            da.Fill(dt)
                        End Using
                    End Using

                    DropDownList1.DataSource = dt
                    DropDownList1.DataTextField = "FullInfo"
                    DropDownList1.DataValueField = "FullInfo"
                    DropDownList1.DataBind()

                    DropDownList3.DataSource = dt
                    DropDownList3.DataTextField = "FullInfo"
                    DropDownList3.DataValueField = "FullInfo"
                    DropDownList3.DataBind()

                    DropDownList4.DataSource = dt
                    DropDownList4.DataTextField = "FullInfo"
                    DropDownList4.DataValueField = "FullInfo"
                    DropDownList4.DataBind()

                    DropDownList5.DataSource = dt
                    DropDownList5.DataTextField = "FullInfo"
                    DropDownList5.DataValueField = "FullInfo"
                    DropDownList5.DataBind()

                    DropDownList6.DataSource = dt
                    DropDownList6.DataTextField = "FullInfo"
                    DropDownList6.DataValueField = "FullInfo"
                    DropDownList6.DataBind()

                    DropDownList7.DataSource = dt
                    DropDownList7.DataTextField = "FullInfo"
                    DropDownList7.DataValueField = "FullInfo"
                    DropDownList7.DataBind()
                    con.Close()
                Next 

2 个答案:

答案 0 :(得分:0)

Dim SqlQuery As String = "SELECT [First Name] + ' ' + [Last Name] + ' ' + [Email] + ' ' + [Phone] AS [FullInfo] FROM [Official] ORDER BY [Last Name]"

答案 1 :(得分:0)

我能够通过创建数组列表并对其进行排序来解决我的问题。这可以在http://codeintheoven.blogspot.com/2012/01/what-are-we-cooking-today-one-way-of.html

找到

此示例代码如下所示:

Private Sub SortDropDown(ByVal ddl As DropDownList)


    '---Get listItems from dropDownList
    Dim ddlList As New ArrayList
    For Each li As ListItem In ddl.Items
        ddlList.Add(li)
    Next

    '---Sort arraylist
    ddlList.Sort(New ListItemComparer)


    '---Copy sorted list back into the dropDownList
    ddl.Items.Clear()
    For Each li As ListItem In ddlList
        ddl.Items.Add(li)
    Next

End Sub

AND这个:

Public Class ListItemComparer : Implements IComparer


Public Function Compare(ByVal x As Object, _
      ByVal y As Object) As Integer _
      Implements IComparer.Compare
    Dim a As ListItem = x
    Dim b As ListItem = y
    Dim c As New CaseInsensitiveComparer
    Return c.Compare(a.Text, b.Text)
End Function

End Class