我的页面上有一个GridView,我想将数据绑定到,并在显示GridView之前进行一些排序但没有显示任何内容。以下是我的代码:
Dim dt As New DataTable
Dim dr As DataRow
' define the table's schema
dt.Columns.Add(New DataColumn("PP", GetType(String)))
dt.Columns.Add(New DataColumn("-", GetType(String)))
dt.Columns.Add(New DataColumn("Distance", GetType(String)))
Dim i As Integer = 0
' Now Create a loop that Add data into datagridview and sort by shortest distance
For Each r As System.Guid In arrid
dr = dt.NewRow()
dr("PP") = ResolveUrl(arrpic(i))
dr("-") = Arrnames(i).ToString & " (" & arrage(i) & ")" & vbCrLf & arrgender(i) & " from " & arrlocation(i) & vbCrLf & arrcomments(i)
dr("Distance") = arrdistance(i)
i = +1
Next
GridView1.DataSource = dt.DataSet
GridView1.DataBind()
'SortDirection data by shortest distance
'GridView1.Sort("Distance", SortDirection.Ascending)
答案 0 :(得分:1)
GridView
未显示,因为它是空的。
您尚未将新行添加到DataTable
,因此请使用table.Rows.Add(newRow)
或table.Rows.Add()
而不是返回已添加的(空)行:
For Each r As System.Guid In arrid
Dim dr = dt.NewRow() ' empty row not added yet
dr("PP") = ResolveUrl(arrpic(i))
dr("-") = Arrnames(i).ToString & " (" & arrage(i) & ")" & vbCrLf & arrgender(i) & " from " & arrlocation(i) & vbCrLf & arrcomments(i)
dr("Distance") = arrdistance(i)
dt.Rows.Add(dr) ' now added
i = +1
Next
或
For Each r As System.Guid In arrid
Dim dr = dt.Rows.Add() ' empty row now already added
dr("PP") = ResolveUrl(arrpic(i))
dr("-") = Arrnames(i).ToString & " (" & arrage(i) & ")" & vbCrLf & arrgender(i) & " from " & arrlocation(i) & vbCrLf & arrcomments(i)
dr("Distance") = arrdistance(i)
i = +1
Next
如果可能的话,我更喜欢第二种方式,它会保存一行并防止出现这样的问题。