简体   繁体   中英

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. 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. 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() .

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. First set OnRowDataBound property to the event handler name in aspx code, let's say the name is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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