GridView错误:未将对象引用设置为对象的实例

时间:2012-06-07 12:40:37

标签: asp.net vb.net data-binding gridview drop-down-menu

有一个带有模板字段的gridview,一列包含一个应该用sql语句填充的下拉列表。我已动态创建网格并调用rowdatabound以访问下拉列表,但我一直收到错误:对象引用未设置为对象的实例。有人有什么想法吗?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:CommandField EditText="Add" ShowEditButton="True" />
        <asp:TemplateField>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" />                          
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" />
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" />
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>        
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server" />        
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" />
            </ItemTemplate>
            </asp:TemplateField>
        <asp:TemplateField>        
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" />
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" />        
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <EditItemTemplate>
                <asp:Label ID="Label6" runat="server" />
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label7" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是动态创建GridView的代码,这是在page_load事件中完成的:

If Not Page.IsPostBack Then
        Dim CommittedTable As New DataTable("Committed")

        CommittedTable.Columns.Add("Date Posted", GetType(Date))
        CommittedTable.Columns.Add("Vendor", GetType(String))
        CommittedTable.Columns.Add("Expense Description", GetType(String))
        CommittedTable.Columns.Add("Ledger", GetType(String))
        CommittedTable.Columns.Add("Amount", GetType(String))
        CommittedTable.Columns.Add("Initials", GetType(String))

        For i As Integer = 0 To GridView1.Rows.Count
            Dim tableRow As DataRow = CommittedTable.NewRow()

            tableRow("Date Posted") = Date.Today
            tableRow("Vendor") = ""
            tableRow("Expense Description") = ""
            tableRow("Ledger") = ""
            tableRow("Amount") = ""
            tableRow("Initials") = ""
            CommittedTable.Rows.Add(tableRow)
        Next

        Session("CommsTable") = CommittedTable
        BindDataComm()

    End If

最后,这是RowDataBound事件处理程序代码:

Dim ddl As DropDownList = DirectCast(e.Row.FindControl("DropDownList1"), DropDownList)
    If ddl Is Nothing Then
        result = dbConnect(dbType.SqlServer, ConfigurationManager.AppSettings("SQLServerConnection"))
        If result = "Successful" Then
            dt = FillDataTable(dbType.SqlServer, "SELECT V_VendorNo + ' | ' + V_VendorName FROM VendorTbl")
            ddl.DataSource = dt 'it errors out here'
            ddl.DataTextField = "V_VendorNo"
            ddl.DataValueField = "V_VendorName"
            ddl.DataBind()
        End If
    End If

1 个答案:

答案 0 :(得分:1)

在绑定RowDatabound事件中的数据之前,您是否正在检查Datarow的RowType,如果没有请检查....