簡體   English   中英

Gridview數據綁定下拉列表和更新

[英]Gridview databound drop down list and updating

  • .NET 4.51,正在使用C#連接器從mySQL數據庫檢索數據
  • 我正在嘗試將數據綁定下拉列表與網格視圖一起使用。 我可以填充下拉列表,但無法弄清楚如何在OnRowUpdating中更新基礎數據集中的值

網格:

<asp:GridView ID="grdProductPrices" runat="server" AllowPaging="True" AllowSorting="True" DataKeyNames="ID" DataSourceID="dscProductPriceMaintenance" AutoGenerateColumns="False" BorderStyle="None" BorderWidth="0px" 
                CssClass="table table-bordered table-condensed table-hover table-striped" EnableTheming="True" PageSize="30"
                OnRowDataBound="grdProductPrices_OnRowDataBound"    
                OnRowUpdating="grdProductPrices_OnRowUpdating"
                >
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:TemplateField HeaderText="ID" SortExpression="ID">
                        <ItemTemplate>
                            <asp:Label ID="lblProductID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Type">
                        <EditItemTemplate>
                            <asp:Label ID="lblProductType" runat="server" Text='<%# Bind("Type") %>' Visible = "false" />  
                            <asp:DropDownList ID="ddlProductType" runat="server" CssClass="form-control"></asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblProductType" runat="server" Text='<%# Bind("Type") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <PagerTemplate>
                    <nuc:GridPager ID="gridPager" runat="server"
                        ShowFirstAndLast="True"
                        ShowNextAndPrevious="True"
                        PageLinksToShow="10"
                        Position="Right"
                        NextText="Next"
                        PreviousText="Prev"
                        FirstText="First"
                        LastText="Last" />
                </PagerTemplate>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:SqlDataSource ID="dscProductPriceMaintenance" runat="server" ConnectionString="<%$ ConnectionStrings:mysqlConn %>" ProviderName="<%$ ConnectionStrings:mysqlConn.ProviderName %>"
        DeleteCommand="DELETE FROM Product WHERE ID = ?"
        InsertCommand="INSERT INTO Product (ID, Name, Variety, Unit, Price) VALUES (?ID, ?Name, ?Variety, ?Unit, ?Price)"
        SelectCommand="SELECT ID, Comments, Name, Variety, Unit, Price, Type, Available, List_Product FROM Product"
        UpdateCommand="UPDATE Product SET Name = ?Name, Variety = ?Variety, Unit = ?Unit, Price = ?Price, Comments = ?Comments, Available = ?Available, List_Product = ?List_Product WHERE ID = ?ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int64" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Available" Type="Int32" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="ID" Type="Int64" />
            <asp:Parameter Name="List_Product" Type="Int32" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Price" Type="Single" />
            <asp:Parameter Name="Unit" Type="String" />
            <asp:Parameter Name="Variety" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Available" Type="Int32" Direction="Input" />
            <asp:Parameter Name="Comments" Type="String" Direction="Input" />
            <asp:Parameter Name="ID" Type="Int64" Direction="Input" />
            <asp:Parameter Name="List_Product" Type="Int32" Direction="Input" />
            <asp:Parameter Name="Name" Type="String" Direction="Input" />
            <asp:Parameter Name="Price" Type="Single" Direction="Input" />
            <asp:Parameter Name="Unit" Type="String" Direction="Input" />
            <asp:Parameter Name="Variety" Type="String" Direction="Input" />
        </UpdateParameters>
    </asp:SqlDataSource>

並填充下拉列表:

protected void grdProductPrices_OnRowDataBound(object aSender, GridViewRowEventArgs aEventArgs)
{
    if (aEventArgs.Row.RowType == DataControlRowType.DataRow)
    {
        //Populate the drop down list
        DropDownList cmbProductTypes = aEventArgs.Row.FindControl("ddlProductType") as DropDownList;
        if (cmbProductTypes != null)
        {
            IQueryable<ProductType> productTypes = BusinessLayer.Instance()
                                                    .ProductTypesGet();

            cmbProductTypes.DataTextField = "Name";
            cmbProductTypes.DataValueField = "ID";
            cmbProductTypes.DataSource = productTypes;
            cmbProductTypes.DataBind();

            //Set the selected index
            Label label = aEventArgs.Row.FindControl("lblProductType") as Label;
            if (label != null)
            {     
               string productTypeId = label.Text;
               cmbProductTypes.Items.FindByValue(productTypeId).Selected = true;
            }

         }
    }
}

然后更新所選值:

protected void grdProductPrices_OnRowUpdating(object aSender, GridViewUpdateEventArgs aEventArgs)
{
     DropDownList cmbProductTypes = grdProductPrices.Rows[aEventArgs.RowIndex].FindControl("ddlProductType") as DropDownList;
     if (cmbProductTypes != null)
     {
         DataRowView dr = grdProductPrices.Rows[aEventArgs.RowIndex].DataItem as DataRowView;
         if (dr != null) dr["Type"] = cmbProductTypes.SelectedValue;
     }
 }

但是在上面的dr總是NULL? 那么,我該怎么做才能獲取記錄中的“類型”字段以使用下拉列表中的值進行更新?

感謝幫助。

DataItem屬性僅在GridView控件的RowDataBound事件期間和之后才可用。

根據文檔,在OnRowUpdating事件上, DataItem將為null

嘗試下面

e.NewValues["Type"] = cmbProductTypes.SelectedValue;

您在dr中獲得了空值,因為在OnRowUpdating事件中DataItem將為空,這與@Damith上面說的相同。為什么為空,您可以在此處觀看。

試試吧

protected void grdProductPrices_OnRowUpdating(object aSender, GridViewUpdateEventArgs aEventArgs)
{
    DropDownList cmbProductTypes = grdProductPrices.Rows[aEventArgs.RowIndex].FindControl("ddlProductType") as DropDownList;
    if (cmbProductTypes != null)
    {
        e.NewValues["Type"] = cmbProductTypes.SelectedValue;
    }
}

有關更多信息,請參見NewValues文檔。

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM