简体   繁体   中英

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. First you need to add the column and OnRowDataBound="gvProduct_RowDataBound" in the aspx code:

<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:

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.

See here for more information regarding RowDataBound event.

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.

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