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

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

本文介绍了以编程方式在网格视图列上显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

谢谢.

已编辑部分

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

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>

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

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

注意:gvProduct_RowDataBound将对数据源中的每一行执行,因此,如果distSPUItem有10个项目,则gvProduct_RowDataBound将被执行10次,同时将e.Row.RowIndex的值从0开始递增.上面的代码仅当productQuantityListdistSPUItem具有相同数量的项目时才有效,否则会出错.

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.

请参见此处以获取有关RowDataBound事件的更多信息.

See here for more information regarding RowDataBound event.

这篇关于以编程方式在网格视图列上显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:14