如何在运行期间在gridview中的值中放置值?

时间:2010-06-14 08:00:30

标签: c# asp.net

如何在运行时在gridview中的Label中放置值?我想在pageload期间在gridview中的标签中放置动态值。这是我的示例代码

    private void SplitStrings()
    {
        List<string> listvalues = new List<string>();
        listvalues = (List<string>)Session["mylist"];
        string[] strvalues = listvalues.ToArray();

        if (listvalues != null)
        {
            foreach (string strElement in listvalues)
            { 
                string[] test = strElement.ToString().Split("|".ToCharArray());
                string prodQuantity = test[0].ToString();
                foreach (GridView row in gvOrderProducts.Rows)
                {
                    prodQuantity = ((Label)row.FindControl("lblQuantity")).Text;
                }
            }
        }
    }

我希望 lblQuantity 标签在我的gridview中显示数量值

3 个答案:

答案 0 :(得分:1)

List<double> productQty = //Filled with your quantity values
        int i = 0;
        foreach (GridViewRow row in grvProducts.Rows)
        {
            ((Label)row.FindControl("lblQuantity")).Text = productQty[i];
            i++;
        }

答案 1 :(得分:-1)

private void SplitStrings()
{
    List<string> listvalues = new List<string>();
    listvalues = (List<string>)Session["mylist"];
    string[] strvalues = listvalues.ToArray();

    if (listvalues != null)
    {
        foreach (string strElement in listvalues)
        { 
            string[] test = strElement.ToString().Split("|".ToCharArray());
            string prodQuantity = test[0].ToString();
            foreach (GridView row in gvOrderProducts.Rows)
            {
                prodQuantity = ((Label)row.FindControl("lblQuantity")).Text;
            }
        }
    }
}

答案 2 :(得分:-1)

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="productDataSource" Runat="server"
            SelectCommand="SELECT [ProductName], [ProductID], 
            [UnitPrice], [UnitsInStock] FROM [Products]"
            UpdateCommand="UPDATE [Products] SET [ProductName] = 
              @ProductName, [UnitPrice] = @UnitPrice, [UnitsInStock] = 
              @UnitsInStock WHERE [ProductID] = @original_ProductID"
            ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
            <UpdateParameters>
                <asp:Parameter Type="String" 
                  Name="ProductName"></asp:Parameter>
                <asp:Parameter Type="Decimal" 
                  Name="UnitPrice"></asp:Parameter>
                <asp:Parameter Type="Int16" 
                  Name="UnitsInStock"></asp:Parameter>
                <asp:Parameter Type="Int32" Name="ProductID"></asp:Parameter>
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" Runat="server" 
            DataSourceID="productDataSource" DataKeyNames="ProductID"
            AutoGenerateColumns="False" AllowPaging="True" 
            BorderWidth="1px" BackColor="White"
            CellPadding="4" BorderStyle="None" BorderColor="#3366CC">
            <FooterStyle ForeColor="#003399" 
               BackColor="#99CCCC"></FooterStyle>
            <PagerStyle ForeColor="#003399" HorizontalAlign="Left" 
               BackColor="#99CCCC"></PagerStyle>
            <HeaderStyle ForeColor="#CCCCFF" Font-Bold="True" 
               BackColor="#003399"></HeaderStyle>
            <Columns>
                <asp:CommandField ShowEditButton="True"></asp:CommandField>
                <asp:BoundField ReadOnly="True" HeaderText="ProductID" 
                    InsertVisible="False" DataField="ProductID"
                    SortExpression="ProductID"></asp:BoundField>
                <asp:BoundField HeaderText="Product" 
                    DataField="ProductName" 
                    SortExpression="ProductName"></asp:BoundField>
                <asp:BoundField HeaderText="Unit Price" 
                    DataField="UnitPrice" SortExpression="UnitPrice">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField HeaderText="Units In Stock" 
                   DataField="UnitsInStock" SortExpression="UnitsInStock">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
                </asp:BoundField>
            </Columns>
            <SelectedRowStyle ForeColor="#CCFF99" Font-Bold="True" 
                BackColor="#009999"></SelectedRowStyle>
            <RowStyle ForeColor="#003399" BackColor="White"></RowStyle>
        </asp:GridView>

    </div>
    </form>
</body>
</html>