簡體   English   中英

以編程方式在網格視圖列上顯示數據

[英]Display data on grid view column programmatically

我有一個產品數量列表和一個網格視圖。 網格視圖已綁定到某些數據。 但是我想在網格視圖的第三列顯示產品數量列表。 以下是有關如何將數據綁定到網格視圖的代碼:

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

如果在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將在從0開始遞增e.Row.RowIndex值的同時執行10次。以上代碼僅在productQuantityListdistSPUItem具有相同數量的商品,否則將是錯誤的。

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

然后將“產品項”列表轉換為“產品擴展項”列表:

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

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

最后,將該新列表綁定到您的網格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM