在页面加载时显示GridView

时间:2014-02-24 16:26:05

标签: c# asp.net .net gridview

我想手动向GridView添加行,并在Page Load中显示它。出于某种原因,我当前的代码显示一个空的GridView。

任何帮助将不胜感激!谢谢!

Default.aspx的

     <asp:GridView ID="AllocationGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"
                        Width="691px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
                        AutoPostBack="true">
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
                        <Columns>
                            <asp:TemplateField HeaderText="Asset Class">
                                <ItemStyle Font-Size="13px" Width="20%" />
                                <ItemTemplate>
                                    <asp:Label ID="AssetLabel" runat="server" ReadOnly="true" Text="" BorderWidth="0px"
                                        Style="text-align: left;"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Weight">
                                <ItemStyle Font-Size="13px" Width="20%" />
                                <ItemTemplate>
                                    <asp:TextBox ID="WeightTextBox" runat="server" ReadOnly="true" BorderWidth="0px"
                                        Style="text-align: left;"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EditRowStyle BackColor="#EBEBEB" />
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    </asp:GridView>

Default.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FirstAllocationGridViewRow();
        }           

    }

    protected void FirstAllocationGridViewRow()
    {
        DataTable table = new DataTable();

        string[] assets = new string[] { "Cash", "US Equity", "Fixed Income", "BAS", "International" };

        table.Columns.Add(new DataColumn("Col1", typeof(string)));
        table.Columns.Add(new DataColumn("Col2", typeof(double)));

        DataRow dr = table.NewRow();


        for (int i = 0; i < assets.Count(); i++)
        {
            dr = table.NewRow();

            dr["Col1"] = assets[i];
            dr["Col2"] = DBNull.Value;

            table.Rows.Add(dr);
        }

        ViewState["currentAllocationTable"] = table;

        AllocationGridView.Visible = true;
        AllocationGridView.DataSource = table;
        AllocationGridView.DataBind();
    }

3 个答案:

答案 0 :(得分:1)

您的cs代码正确问题在于您的布局代码。 文本属性未绑定到表字段名称

Text='<%# Bind("Col1") %>'

这是完整的gridview

<asp:GridView ID="AllocationGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"
                    Width="691px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
                    AutoPostBack="true">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
                    <Columns>
                        <asp:TemplateField HeaderText="Asset Class">
                            <ItemStyle Font-Size="13px" Width="20%" />
                            <ItemTemplate>
                                <asp:Label ID="AssetLabel" runat="server" ReadOnly="true" Text='<%# Bind("Col1") %>' BorderWidth="0px"
                                    Style="text-align: left;"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Weight">
                            <ItemStyle Font-Size="13px" Width="20%" />
                            <ItemTemplate>
                                <asp:TextBox ID="WeightTextBox" runat="server" Text='<%# Bind("Col2") %>' ReadOnly="true" BorderWidth="0px"
                                    Style="text-align: left;"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#EBEBEB" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>

答案 1 :(得分:0)

Remove AutoPostBack="true"控件中的Gridview字段。并确保数据表具有正确的列值,请参阅Text=""标签中的gridview。您需要为标签指定值。

请参阅此link,它可能会帮助您

答案 2 :(得分:-2)

您的问题在于您的模板字段,因为您无法使用DataTable为其设置值,将其更改为BoundFields或设置AutoGenerateColumns="True" 用我的答案尝试了你的代码,它工作正常。