【问题标题】:Display data on grid view column programmatically以编程方式在网格视图列上显示数据
【发布时间】:2014-01-01 15:05:06
【问题描述】:

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

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

【问题讨论】:

  • distSPUItemproductQuantityList的类型是什么?
  • distSPUItem 是从一个类 List distSPUItem = new List(); 中检索的。 productQuantityList 是整数类型。我是否以错误的方式定义新列?

标签: c# asp.net gridview


【解决方案1】:

如果你在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 将执行 10 次,同时将 e.Row.RowIndex 的值从 0 开始递增。上面的代码只有productQuantityListdistSPUItem 有相同数量的项目才会起作用,否则会出错。

有关RowDataBound 事件的更多信息,请参阅here

【讨论】:

  • 我得到了 1,2,1,1,2,2,2,2,2,2 的 productQuantityList 列表。但是,当我在 RowDataBound 方法中执行时,它会返回所有 2,2,2... 你能帮我检查编辑的部分吗?
  • 我必须先将数量存储在列表中,因为该列表是从我的代码的其他部分检索到的,并且非常复杂。
  • 你是说第一行应该是1,第二行应该是2,第三和第四应该是1,剩下的应该是2?
  • 是的,productQuantityList 是整数
  • 它有效。而且,当我将项目添加到 distSPUItem 时,我将数量添加到 productQUantityList 以及我的其他代码部分。但是您介意稍微解释一下 RowDataBound 方法吗?
【解决方案2】:

我会尝试这样解决问题:

假设您有一个产品项目,如下所示:

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

然后将 Product 项列表转换为 ProductExtended 项列表:

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

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

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

【讨论】:

  • 感谢您的努力,我想尝试一下。但是,我的其他部分代码有一堆逻辑来获取 productQuantityList 中的项目,我无法更改它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
相关资源
最近更新 更多