Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式在“栅格视图”列上显示数据_C#_Asp.net_Gridview - Fatal编程技术网

C# 以编程方式在“栅格视图”列上显示数据

C# 以编程方式在“栅格视图”列上显示数据,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个产品数量列表和一个网格视图。网格视图已绑定到某些数据。但是我想在grid view的第三列显示产品数量列表。下面是关于如何将数据绑定到网格视图的代码: gvProduct.DataSource = distSPUItem; gvProduct.DataBind(); BoundField column = new BoundField(); column = new BoundField(); column.HeaderText = "Unit Quantity"; for (int

我有一个产品数量列表和一个网格视图。网格视图已绑定到某些数据。但是我想在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);
gvProduct.DataSource=distspubitem;
gvProduct.DataBind();
BoundField列=新的BoundField();
列=新的边界字段();
column.HeaderText=“单位数量”;
for(int index=0;index
我需要循环浏览产品数量列表,并在网格视图的第三列显示结果。但是,该列不会显示。有什么解决办法吗

提前谢谢

编辑部分

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();
        }
    }
受保护的void gvProduct_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
整数单位数量=0;
如果(e.Row.RowType==DataControlRowType.DataRow)
{
for(int index=0;index
如果在
RowDataBound
事件中执行此操作,效果会更好。首先,您需要在aspx代码中添加列和
OnRowDataBound=“gvProduct\u 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
将对数据源中的每一行执行,因此,如果
distSPUItem
有10个项目,则
gvProduct_RowDataBound
将执行10次,同时从0开始递增
e.row.rowdindex
值。只有当
productQuantityList
distSPUItem
的项数相同时,上述代码才有效,否则将出错


有关
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;
    }
}
如果需要其他信息,例如某种索引,请创建如下类:

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();
List products=newlist();
// ...
int i=0;
列出产品扩展=产品。选择(p=>newproductextended(p,i++)。ToList();

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

distSPUItem和productQuantityList的类型是什么?distSPUItem是从类中检索的,list distSPUItem=new list();。productQuantityList是整数类型。我是不是用错误的方式定义了新列?我得到了一个productQuantityList的列表,是1,2,1,1,2,2,2,2,2。但是,当我在RowDataBound方法中执行此操作时,它将返回所有2,2,2。。。你能帮我检查一下编辑过的部分吗?我必须先在列表中存储数量,因为列表是从我的代码的其他部分检索的,而且非常复杂。你是说第一行应该是1,第二行应该是2,第三行和第四行应该是1,其余的应该是2?是的productQuantityList是整数,但它可以工作。是的,当我将项目添加到distSPUItem中时,我将数量添加到productQUantityList中,以及代码的其他部分。但是你介意解释一下RowDataBound方法吗?谢谢你的努力,我想试试。然而,我的另一部分代码有一系列逻辑来获取productQuantityList中的项目,我无法更改它们。