我在网格视图中遇到了一些问题。我有一个列为5的网格视图。当我选择从顶部添加项目时,底部的网格视图将刷新。同时,它从数据库中检索一些数据并将其显示在网格视图的文本框中。这是我的按钮动作事件方法:
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;
}
然而,它只是让我回来0.我确实检查了SQL语句,都返回了正确的值。有什么方法可以解决这个问题吗?
提前致谢。
修改
<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>
答案 0 :(得分:1)
问题是您在绑定数据之前遍历gvFinalised
行,因此当您调用gvFinalised.DataBind()
时,无论您在循环中执行什么操作都会被覆盖。
移动它:
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();
如下面的@SutharMonil所述,使用RowDataBound事件可以采用另一种方法。首先将OnRowDataBound
属性设置为aspx代码中的事件处理程序名称,假设名称为gvFinalised_RowDataBound
:
<asp:GridView ID="gvFinalised" runat="server" AutoGenerateColumns="False" CellPadding="2"
ForeColor="#333333" GridLines="None" Width="740px" DataKeyNames="id" OnRowDataBound="gvFinalised_RowDataBound">
然后在代码后面,删除以下代码:
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();
}
并添加以下代码:
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();
}
}
这样,只要调用gvFinalised
,gvFinalised.DataBind()
内的数量文本框就会始终更新。