如何在网格视图内的下拉列表中显示数据库中的值

本文关键字:下拉列表 数据库 显示 网格 视图 | 更新日期: 2023-09-27 18:32:13

我正在尝试从数据库获取值到放置在网格视图项模板内的下拉列表。我正在使用网格视图从用户那里获取值。在一列中,我正在使用下拉列表,用户必须从中选择一项。根据选择,其成本价将自动填充到另一列中。但是我无法获取下拉列表中的值并收到错误"对象引用未设置为对象的实例"。

Aspx 代码如下:

<asp:GridView ID="gvItemList" runat="server" ShowFooter="True" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" ViewStateMode="Enabled" CssClass="newStyle9" style="text-align: center" OnRowDeleting="gvItemList_RowDeleting" OnRowDataBound="gvItemList_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Sl No" SortExpression="Id">                                    
                                    <ItemTemplate>
                                        <asp:Label ID="lblId" runat="server"
                                            Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
         <asp:TemplateField HeaderText="Item">
             <ItemTemplate>
                 <asp:DropDownList ID="ddlItem" runat="server" Height="25px" Width="128px">
                 </asp:DropDownList>
             </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Required Date">
            <ItemTemplate>
                <asp:TextBox ID="txtRequiredDate" runat="server" />
                <ajaxToolkit:CalendarExtender ID="txtRequiredDate_CalendarExtender" runat="server" Enabled="True" TargetControlID="txtRequiredDate">
                </ajaxToolkit:CalendarExtender>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Required Quantity">
            <ItemTemplate>
                <asp:TextBox ID="txtRequiredQuantity" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Cost Price">
            <ItemTemplate>
                <asp:Label ID="lblCostPrice" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>        
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="lblTotal" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="UoM Code">
            <ItemTemplate>
                <asp:Label ID="lblUomCode" runat="server">Manual</asp:Label>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
                <asp:Button ID="AddRowButton" runat="server" Text="Add New Item" 
                    OnClick="ButtonAdd_Click" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
    <RowStyle BackColor="White" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>

行数据绑定下的代码给出如下:

protected void gvItemList_RowDataBound(object sender, GridViewRowEventArgs e)
    {        
        DS_SiteDataTableAdapters.tbl_ItemTableAdapter item;
        item = new DS_SiteDataTableAdapters.tbl_ItemTableAdapter();
        DataTable dt = new DataTable();
        dt = item.GetItem();
        DropDownList ddlItem = (DropDownList)e.Row.FindControl("ddlItem");
        ddlItem.DataSource = dt; //here I'm getting an error "Object reference not set to an instance of an object."
        ddlItem.DataTextField = "Item";
        ddlItem.DataValueField = "Item";
        ddlItem.DataBind();        
    }

任何帮助将不胜感激!

如何在网格视图内的下拉列表中显示数据库中的值

if (e.Row.RowType == DataControlRowType.DataRow)
{
    //Find the DropDownList in the Row
    DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
    ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers");
    ddlCountries.DataTextField = "Country";
    ddlCountries.DataValueField = "Country";
    ddlCountries.DataBind();
    //Add Default Item in the DropDownList
    ddlCountries.Items.Insert(0, new ListItem("Select Country"));
}

希望这有帮助!

只需检查方法中的以下条件

if (e.Row.RowType == DataControlRowType.DataRow)

        { your code }

我希望它能帮助你

如注释中所述,您正在尝试使用 null 对象,认为它有一个引用的对象。

你正在做这样的事情:

public class Example
{
   public void MyMethod()
   {
   }
}
Example myExample= null;
myExample.MyMethod();

您将获得与NullReferenceException相同的"Object reference not set to an instance of an object.",因为您正在对 null 的引用类型调用方法。

收到Object Reference错误,因为您没有在网格视图中检查DataRow。因此,它试图在标题中查找不存在的下拉列表,因此您将下拉列表对象设置为Null。

将此条件添加到您的gvItemList_RowDataBound事件中:-

protected void gvItemList_RowDataBound(object sender, GridViewRowEventArgs e)
{ 
if (e.Row.RowType == DataControlRowType.DataRow)
   {
      //Your rest code
   }
}

试试这个你会得到空引用,因为你找不到 ddlItem 控件

在行 DropDownList ddlItem = (DropDownList)e.Row.FindControl("ddlItem");

所以你可以试试下面 DropDownList ddlItem = (DropDownList)GridView.Rows[rowindex].FindControl("ddlItem"))