简体   繁体   English

gridview中的文本框未检索数据

[英]TextBox in gridview not retrieving data

I am having some problem with the grid view. 我在网格视图上遇到了一些问题。 I have a grid view with column of 5. When I select add item from top, the grid view at the bottom will be refresh. 我有一个列为5的网格视图。当我从顶部选择添加项目时,底部的网格视图将被刷新。 At the same time, it is retrieving some data from the database and display it in the text box in grid view. 同时,它正在从数据库中检索一些数据并将其显示在网格视图的文本框中。 Here is my button action event method: 这是我的按钮动作事件方法:

protected void lbnAdd_Click(object sender, EventArgs e)
    {
        List<ProductPacking> prodVariantDetail = new List<ProductPacking>();

        // get the last product variant IDs from ViewState
        prodVariantIDList = this.SelectedVariantDetailIDs;

        foreach (RepeaterItem ri in Repeater1.Items)
        {
            GridView gvProduct = (GridView)ri.FindControl("gvProduct");
            foreach (GridViewRow gr in gvProduct.Rows)
            {
                CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
                if (cb.Checked)
                {
                    // add the corresponding DataKey to idList
                    prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
                }
            }
        }
        for (int i = 0; i < prodVariantIDList.Count; i++)
        {
            prodVariantDetail.Add(prodPackBLL.getProdVariantDetailByID(prodVariantIDList[i]));

            foreach (GridViewRow gr in gvFinalised.Rows)
            {
                //Get the product packaging quantity by productName
                string name = gr.Cells[2].Text;
                int productQuantity = packBLL.getProductQuantityByName(name);
                TextBox tb = (TextBox)gr.Cells[5].FindControl("tbQuantity");
                tb.Text = productQuantity.ToString();
            }
        }

        gvFinalised.DataSource = prodVariantDetail;
        gvFinalised.DataBind();

        // save prodVariantIDList to ViewState
        this.SelectedVariantDetailIDs = prodVariantIDList;
    }

However, it just keep returning me 0. I did check the SQL statement, all returning me the correct values. 但是,它只是一直向我返回0。我确实检查了SQL语句,都向我返回了正确的值。 Is there any way to fix this? 有没有什么办法解决这一问题?

Thanks in advance. 提前致谢。

EDIT 编辑

<asp:GridView ID="gvFinalised" runat="server" AutoGenerateColumns="False" CellPadding="2" ForeColor="#333333" GridLines="None" Width="740px" DataKeyNames="id">
                            <Columns>
                                <asp:BoundField DataField="id" HeaderText="ID" ItemStyle-Width="50px" />
                                <asp:BoundField DataField="categoryName" HeaderText="Category" ItemStyle-Width="100px" />
                                <asp:BoundField DataField="name" HeaderText="Product" ItemStyle-Width="350px" />
                                <asp:BoundField DataField="inventoryQuantity" HeaderText="Stock" ItemStyle-Width="100px" />
                                <asp:BoundField DataField="unitQuantity" HeaderText="Unit" ItemStyle-Width="100px" />
                                <asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="200px">
                                    <ItemTemplate>
                                        <asp:TextBox ID="tbQuantity" runat="server" Width="40" Text="0" OnTextChanged="tbQuantity_TextChanged" AutoPostBack="true" />
                                        <asp:Label ID="lblCheckAmount" runat="server" ForeColor="#a94442"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>

The problem is you loop through gvFinalised rows before binding the data, so whatever you do inside the looping will be overwritten when you call gvFinalised.DataBind() . 问题是您绑定数据之前循环遍历gvFinalised行,因此,当您调用gvFinalised.DataBind()时,在循环内所做的任何操作都将被覆盖。

Quick solution 快速解决方案

Move this: 移动这个:

foreach (GridViewRow gr in gvFinalised.Rows)
{
    //Get the product packaging quantity by productName
    string name = gr.Cells[2].Text;
    int productQuantity = packBLL.getProductQuantityByName(name);
    TextBox tb = (TextBox)gr.Cells[5].FindControl("tbQuantity");
    tb.Text = productQuantity.ToString();
}

after this: 在这之后:

gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();


Alternative Solution 替代解决方案

As mentioned by @SutharMonil below, there's an alternative way to do this using RowDataBound event. 如下面的@SutharMonil所提到的,还有一种使用RowDataBound事件来执行此操作的替代方法。 First set OnRowDataBound property to the event handler name in aspx code, let's say the name is gvFinalised_RowDataBound : 首先在aspx代码中将OnRowDataBound属性设置为事件处理程序名称,假设名称为gvFinalised_RowDataBound

<asp:GridView ID="gvFinalised" runat="server" AutoGenerateColumns="False" CellPadding="2"
        ForeColor="#333333" GridLines="None" Width="740px" DataKeyNames="id" OnRowDataBound="gvFinalised_RowDataBound">

then in code behind, remove the following code: 然后在后面的代码中,删除以下代码:

foreach (GridViewRow gr in gvFinalised.Rows)
{
    //Get the product packaging quantity by productName
    string name = gr.Cells[2].Text;
    int productQuantity = packBLL.getProductQuantityByName(name);
    TextBox tb = (TextBox)gr.Cells[5].FindControl("tbQuantity");
    tb.Text = productQuantity.ToString();
}

and add the following code: 并添加以下代码:

protected void gvFinalised_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Display the company name in italics.
        e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";

        //Get the product packaging quantity by productName
        string name = e.Row.Cells[2].Text;
        int productQuantity = packBLL.getProductQuantityByName(name);
        TextBox tb = (TextBox)e.Row.Cells[5].FindControl("tbQuantity");
        tb.Text = productQuantity.ToString();
    }
}

This way the quantity textbox inside gvFinalised will always be updated whenever gvFinalised.DataBind() is called. 这样,里面的数量文本框gvFinalised时将始终更新gvFinalised.DataBind()被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM