2014-09-06 57 views
1

我在我的页面上有一个GridView,我想将数据绑定到数据,并在显示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

简单的事情是设置断点并检查数据表或数据集是否为空... – 2014-09-06 19:21:12

+0

'GridView1.DataSource = dt' – OneFineDay 2014-09-06 19:25:19

+0

@Ganesh_Devlekar。我试图用这一行来计算表格:MsgBox(dt.DataSet.Tables.Count),但我没有将对象引用设置为对象消息的实例,表明没有表格存在。我不知道为什么? – kilojoules88 2014-09-06 19:44:18

回答

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 

我喜欢第二种方法,如果可能的话,它保存一行,并防止这样的问题。

+0

谢谢蒂姆,它的工作 – kilojoules88 2014-09-06 19:56:36

相关问题