C# 在页面加载时显示GridView

C# 在页面加载时显示GridView,c#,asp.net,.net,gridview,C#,Asp.net,.net,Gridview,我想手动将行添加到GridView,并在页面加载时显示它。出于某种原因,我当前的代码显示一个空的GridView Default.aspx Default.aspx.cs 您的问题在于模板字段,因为无法使用DataTable为其设置值、将其更改为BoundFields或设置AutoGenerateColumns=True 用我的答案尝试了你的代码,效果很好。删除Gridview控件中的AutoPostBack=true字段。并确保数据表中的列具有正确的值,请查看gridview标签中的Text=

我想手动将行添加到GridView,并在页面加载时显示它。出于某种原因,我当前的代码显示一个空的GridView

Default.aspx

Default.aspx.cs


您的问题在于模板字段,因为无法使用DataTable为其设置值、将其更改为BoundFields或设置AutoGenerateColumns=True 用我的答案尝试了你的代码,效果很好。

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

看到这一点,它可能会帮助您

您的cs代码很好问题出在您的布局代码中。文本属性未绑定到表字段名

这是完整的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>

@TimSchmelter不知道你的意思,抱歉。GridView中使用的两个控件是一个标签和一个文本框我是asp.net的初学者,我相信我会遵循您的建议。。你能为你的建议提供一个代码快照吗?非常感谢。我在GridView的标签和文本框中分别添加了:Text=和'Text=,现在它可以工作了!这就是你的意思吗?
    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();
    }
Text='<%# Bind("Col1") %>'
<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>