แสดงข้อมูลบนคอลัมน์มุมมองกริดโดยทางโปรแกรม

ฉันมีรายการปริมาณผลิตภัณฑ์และมุมมองตาราง มุมมองตารางเชื่อมโยงกับข้อมูลบางส่วนแล้ว แต่ฉันต้องการแสดงรายการปริมาณผลิตภัณฑ์ที่คอลัมน์ที่สามของมุมมองตาราง นี่คือโค้ดเบื้องหลังเกี่ยวกับวิธีผูกข้อมูลกับมุมมองกริด:

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

person Community    schedule 01.01.2014    source แหล่งที่มา
comment
distSPUItem และ productQuantityList เป็นประเภทใด   -  person ekad    schedule 01.01.2014
comment
distSPUItem ถูกดึงมาจากคลาส List‹DistributionStandardPackingUnitItems› distSPUItem = new List‹DistributionStandardPackingUnitItems›(); productQuantityList เป็นประเภทจำนวนเต็ม ฉันกำหนดคอลัมน์ใหม่ผิดหรือเปล่า?   -  person    schedule 01.01.2014


คำตอบ (2)


มันจะดีกว่าถ้าคุณทำในเหตุการณ์ RowDataBound ก่อนอื่นคุณต้องเพิ่มคอลัมน์และ OnRowDataBound="gvProduct_RowDataBound" ในโค้ด aspx:

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

จากนั้นตั้งค่าข้อความเป็น lblUnitQuantity ในวิธี gvProduct_RowDataBound ในโค้ดด้านหลัง:

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 โค้ดด้านบนจะใช้ได้ก็ต่อเมื่อ productQuantityList และ distSPUItem มีตัวเลขเท่ากัน ของรายการมิฉะนั้นจะเกิดข้อผิดพลาด

ดู ที่นี่ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ RowDataBound กิจกรรม

person ekad    schedule 01.01.2014
comment
ฉันได้รายการ productQuantityList เป็น 1,2,1,1,2,2,2,2,2,2. อย่างไรก็ตาม เมื่อฉันใช้วิธี RowDataBound มันจะคืนค่าทั้งหมด 2,2,2... คุณช่วยฉันตรวจสอบส่วนที่แก้ไขได้ไหม - person ; 01.01.2014
comment
ฉันต้องเก็บปริมาณไว้ในรายการก่อนเนื่องจากรายการถูกดึงมาจากส่วนอื่น ๆ ของโค้ดของฉันและมันซับซ้อนมาก - person ; 01.01.2014
comment
คุณกำลังบอกว่าแถวแรกควรเป็น 1, แถวที่สองควรเป็น 2, แถวที่สามและสี่ควรเป็น 1 และที่เหลือควรเป็น 2? - person ekad; 01.01.2014
comment
ใช่แล้ว productQuantityList นั้นเป็นจำนวนเต็ม - person ; 01.01.2014
comment
มันได้ผล. และเมื่อฉันเพิ่มรายการลงใน distSPUItem ฉันจะเพิ่มปริมาณลงใน productQUAntityList รวมถึงในส่วนอื่นๆ ของโค้ดด้วย แต่คุณช่วยอธิบายเล็กน้อยเกี่ยวกับวิธี RowDataBound หน่อยได้ไหม? - person ; 01.01.2014
comment
เพิ่มคำอธิบายในคำตอบของฉัน - person ekad; 01.01.2014

ฉันจะพยายามแก้ไขปัญหาด้วยวิธีนี้:

สมมติว่าคุณมีรายการสินค้า บางอย่างเช่นนี้:

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

จากนั้นแปลงรายการสินค้าเป็นรายการสินค้า ProductExtens:

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

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

และในที่สุดก็ผูกรายการใหม่นั้นเข้ากับกริดของคุณ

person mrzli    schedule 01.01.2014
comment
ขอบคุณสำหรับความพยายามและฉันอยากจะลอง อย่างไรก็ตาม มีตรรกะมากมายสำหรับส่วนอื่นๆ ของโค้ดของฉันในการรับรายการใน productQuantityList และฉันไม่สามารถเปลี่ยนแปลงรายการเหล่านั้นได้ - person ; 01.01.2014