页面加载: -
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Page.Title = "Batches - " & Website
BindBatches()
Catch ex As Exception
Logger.WriteError("Error in Page_Load of Batches/batches.aspx", ex)
End Try
End Sub
此子例程在网格视图中绑定batch_name
Protected Sub BindBatches()
Try
Dim ds As New DataSet()
ds = Dal.ExecuteDataset("select batch_name from tblBatch")
If Not ds Is Nothing AndAlso ds.Tables(0).Rows.Count > 0 Then
gvBatches.DataSource = ds
gvBatches.DataBind()
gvBatches.Visible = True
Else
gvBatches.Visible = False
End If
Catch ex As Exception
Logger.WriteError("Error in BindBatches of Batches/batches.aspx", ex)
End Try
End Sub
这是gridview: -
<asp:GridView ID="gvBatches" runat="server">
<Columns>
<asp:TemplateField HeaderText="Batch Name">
<HeaderStyle Width="40px"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="lblBatchName" runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
和数据绑定事件: -
Protected Sub gvBatches_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBatches.RowDataBound
If e.Row.RowType = ListItemType.Item Or e.Row.RowType = ListItemType.AlternatingItem Then
Dim batchName As Label = e.Row.FindControl("lblBatchName")
batchName.Text = e.Row.DataItem("batch_name")
End If
End Sub
但是当我调试它时会在 - gvBatches.DataBind()
上显示以下异常DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'id'.
我不明白这个id是什么?我的表中没有这样的专栏。
答案 0 :(得分:0)
我建议先将OPTION STRICT
设置为开启。看到此代码(DataItem
是Object
):
batchName.Text = e.Row.DataItem("batch_name")
而是尝试适当地施放它:
Dim row As DataRow = DirectCast(e.Row.DataItem, DataRowView).Row
batchName.Text = row.Field(Of String)("batch_name")
答案 1 :(得分:0)
在您的aspx文件中,您可以将标签声明更改为
<asp:Label ID="lblBatchName" runat="server" Text='<%# Eval("batch_name") %>'>
并且您不需要Protected Sub gvBatches_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBatches.RowDataBound
功能