简体   繁体   English

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

[英]Display data on grid view column programmatically

I have a list of product quantity and a grid view. 我有一个产品数量列表和一个网格视图。 The grid view is already bind to some data. 网格视图已绑定到某些数据。 But I wanted to display the list of product quantity at the third column of grid view. 但是我想在网格视图的第三列显示产品数量列表。 Here is the code behind on how I bind the data to grid view: 以下是有关如何将数据绑定到网格视图的代码:

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);

I need to loop thru the product quantity list and display the result at the third column of grid view. 我需要遍历产品数量列表,并在网格视图的第三列显示结果。 However, the column does not shows up. 但是,该列未显示。 Any solutions? 有什么办法吗?

Thanks in advance. 提前致谢。

Edited Portion 编辑部分

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();
        }
    }

It would be better if you do it in RowDataBound event. 如果在RowDataBound事件中这样做会更好。 First you need to add the column and OnRowDataBound="gvProduct_RowDataBound" in the aspx code: 首先,您需要在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>

Then set the text value of lblUnitQuantity in gvProduct_RowDataBound method in the code behind: 然后在后面的代码中的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();
    }
}

Note: gvProduct_RowDataBound will be executed for each row from the data source, so if distSPUItem has 10 items, then gvProduct_RowDataBound will be executed 10 times while incrementing e.Row.RowIndex value starting from 0. The above code will only work if productQuantityList and distSPUItem have the same number of items, otherwise it will be error. 注意: gvProduct_RowDataBound将针对数据源中的每一行执行,因此,如果distSPUItem具有10个项目,则gvProduct_RowDataBound将在从0开始递增e.Row.RowIndex值的同时执行10次。以上代码仅在productQuantityListdistSPUItem具有相同数量的商品,否则将是错误的。

See here for more information regarding RowDataBound event. 有关RowDataBound事件的更多信息,请参见此处

I would try to solve the problem this way: 我将尝试通过以下方式解决问题:

Say you have a product item, something like this: 假设您有一个产品,例如:

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

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

If you want additional information, such as some kind of index, create class like this: 如果您需要其他信息(例如某种索引),请创建以下类:

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;
    }
}

Then convert list of Product items to a list of ProductExtended items: 然后将“产品项”列表转换为“产品扩展项”列表:

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

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

And finally bind that new list to your grid. 最后,将该新列表绑定到您的网格。

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

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