-1

有一个带有模板字段的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
4

1 回答 1

1

您是否在绑定 RowDatabound 事件中的数据之前检查 Datarow 的 RowType 如果不是请检查它....

于 2012-06-07T13:28:39.840 回答