2014-02-24 130 views
0

我想手動將行添加到GridView,並在頁面加載中顯示它。出於某種原因,我當前的代碼顯示一個空的GridView。在頁面加載時顯示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(); 
    } 
+0

@TimSchmelter不知道你的意思,對不起。在GridView中使用的兩個控件是一個標籤和一個文本框 – Mayou

回答

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

你的問題是與你的模板字段,你不能設置使用一個DataTable值給他們,改變他們到BoundFields或設置AutoGenerateColumns="True" 用我的答案試過你的代碼,它工作正常。

+0

完全是錯誤的傢伙。 –

0

RemoveAutoPostBack="true"字段在Gridview控制。並確保數據表具有正確的列值,請參閱您的gridview標籤中的Text=""。您需要爲標籤賦予值。

看到這個link,這是5月幫助你

+0

我刪除了'AutoPostBack =「true」',它仍然不起作用... – Mayou

+0

查看我的更新回答 –

+0

我是asp.net的初學者,我確信我遵循你的建議。你請給出你的建議的代碼快照?謝謝! – Mayou