如何在gridview内的下拉列表中显示数据库中的值?

时间:2014-11-26 08:33:54

标签: c# asp.net gridview drop-down-menu

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

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();        
    }

非常感谢任何帮助!

5 个答案:

答案 0 :(得分:1)

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"));
}

希望这有帮助!

答案 1 :(得分:0)

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

在线     DropDownList ddlItem =(DropDownList)e.Row.FindControl(&#34; ddlItem&#34;);

所以你可以试试如下      DropDownList ddlItem =(DropDownList)GridView.Rows [rowindex] .FindControl(&#34; ddlItem&#34;))

答案 2 :(得分:0)

如评论中所述,您正在尝试使用null对象,认为它具有引用的对象。

你正在做这样的事情:

public class Example
{
   public void MyMethod()
   {

   }
}

Example myExample= null;
myExample.MyMethod();

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

答案 3 :(得分:0)

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

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


        { your code }

我希望它能帮助你

答案 4 :(得分:0)

您收到Object Reference错误,因为您没有在gridview中检查DataRow。因为它试图在标题中找不到它的下拉列表,因此你将Dropdown对象作为Null。

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

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