繁体   English   中英

以编程方式在网格视图列上显示数据

[英]Display data on grid view column programmatically

我有一个产品数量列表和一个网格视图。 网格视图已绑定到某些数据。 但是我想在网格视图的第三列显示产品数量列表。 以下是有关如何将数据绑定到网格视图的代码:

gvProduct.DataSource = distSPUItem;
gvProduct.DataBind();
BoundField column = new BoundField(); 
column = new BoundField();
column.HeaderText = "Unit Quantity";
for (int index = 0; index < productQuantityList.Count; index++)
{
   column.DataField = index.ToString();
}
gvProduct.Columns.Add(column);

我需要遍历产品数量列表,并在网格视图的第三列显示结果。 但是,该列未显示。 有什么办法吗?

提前致谢。

编辑部分

protected void gvProduct_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        int unitQuantity = 0;
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            for(int index = 0; index < productQuantityList.Count; index++)
            {
                unitQuantity = productQuantityList[index];
            }
            Label lblUnitQuantity = (Label)e.Row.FindControl("lblUnitQuantity");
            lblUnitQuantity.Text = unitQuantity.ToString();
        }
    }

如果在RowDataBound事件中这样做会更好。 首先,您需要在aspx代码中添加列和OnRowDataBound="gvProduct_RowDataBound"

<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvProduct_RowDataBound">
    <Columns>
        <!-- Existing columns here  -->
        <asp:TemplateField HeaderText="Unit Quantity">
            <ItemTemplate>
                <asp:Label ID="lblUnitQuantity" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

然后在后面的代码中的gvProduct_RowDataBound方法中设置lblUnitQuantity的文本值:

protected void gvProduct_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        int unitQuantity = productQuantityList[e.Row.RowIndex];
        Label lblUnitQuantity = (Label)e.Row.FindControl("lblUnitQuantity");
        lblUnitQuantity.Text = unitQuantity.ToString();
    }
}

注意: gvProduct_RowDataBound将针对数据源中的每一行执行,因此,如果distSPUItem具有10个项目,则gvProduct_RowDataBound将在从0开始递增e.Row.RowIndex值的同时执行10次。以上代码仅在productQuantityListdistSPUItem具有相同数量的商品,否则将是错误的。

有关RowDataBound事件的更多信息,请参见此处

我将尝试通过以下方式解决问题:

假设您有一个产品,例如:

public class Product
{
    public string Data1 { get; set; }
    public string Data2 { get; set; }

    public Product(string data1, string data2)
    {
        Data1 = data1;
        Data2 = data2;
    }
}

如果您需要其他信息(例如某种索引),请创建以下类:

public class ProductExtended
{
    public string Data1 { get; set; }
    public string Data2 { get; set; }
    public int Index { get; set; }

    public ProductExtended(Product product, int index)
    {
        Data1 = product.Data1;
        Data2 = product.Data2;
        Index = index;
    }
}

然后将“产品项”列表转换为“产品扩展项”列表:

List<Product> products = new List<Product>();
// ...

int i = 0;
List<ProductExtended> productsExtended = products.Select(p => new ProductExtended(p, i++)).ToList();

最后,将该新列表绑定到您的网格。

暂无
暂无

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

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