2012-05-16 109 views
2

其實,說問題可能是錯誤的。我有2頁。我可以從第一頁的第二頁獲取查詢字符串。而這個查詢字符串是我查詢的關鍵部分。當調試模式,我可以看到查詢的結果,他們將是我想要的。但它不能顯示在我的GridView上。 這裏是我的代碼塊: 我CustomerList頁,datagridview數據綁定的問題

public partial class CustomerList : System.Web.UI.Page 
    { 
     CustomerBusiness m_CustomerBusiness = new CustomerBusiness(); 
     COMPANY m_Company = new COMPANY(); 

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

     private void BindCustomers() 
       { 
        string CompanyName = Request.QueryString.Get("CompanyName"); 

        LabelCompanyName.Text = CompanyName; 
        List<CUSTOMER> CustomerListt = m_CustomerBusiness.SelectByFirmName(CompanyName); 
        GridViewCustomerList.DataSource = CustomerListt; 
        GridViewCustomerList.DataBind(); 
       } 
} 

GridViewCustomerList:

<asp:GridView ID="GridViewCustomerList" runat="server" 
      AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" 
      BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" 
      ondatabinding="GridViewCustomerList_DataBinding" 
      onselectedindexchanged="GridViewCustomerList_SelectedIndexChanged" 
      Width="239px"> 
      <AlternatingRowStyle BackColor="#DCDCDC" /> 
      <FooterStyle BackColor="#CCCCCC" ForeColor="Black" /> 
      <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#EEEEEE" ForeColor="Black" /> 
      <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" /> 
      <SortedAscendingCellStyle BackColor="#F1F1F1" /> 
      <SortedAscendingHeaderStyle BackColor="#0000A9" /> 
      <SortedDescendingCellStyle BackColor="#CAC9C9" /> 
      <SortedDescendingHeaderStyle BackColor="#000065" /> 
     </asp:GridView> 

CustomerList是我想要的,但我的綁定部分不工作,當我我不能看到GridViewCustomerList運行該項目。我研究了一些asp.net頁面生命週期模型,目標解決方案可能與此有關。

+0

你可以爲你的'GridView'顯示標記嗎? – jadarnel27

+0

如果我能看到它,我會顯示它..卜t正如我所說,我可以看到在調試模式下查詢的結果。 – yiddow

+0

對不起,我指的是'GridViewCustomerList'的標記,因爲它出現在你的ASPX頁面上(在Visual Studio中)。請在您的問題中添加*。 – jadarnel27

回答

3

這裏的問題是,您在標記中沒有明確的<Column>聲明,並且您的AutoGenerateColumns property設置爲「false」。因此,即使數據被綁定到GridView控件,它不知道要顯示什麼(所以什麼也不顯示。

這裏最簡單的解決方法就是刪除AutoGenerateColumns="False"從你的GridView聲明(這是「真」默認情況下),你應該是好去,它會根據你的數據源自動創建列。

或者,您也可以通過添加列部分,你的GridView指定所需的列。

<columns> 
     <asp:boundfield datafield="columnOne" headertext="Column 1"/> 
     <asp:boundfield datafield="columnTwo" headertext="Column 2"/> 
     <asp:boundfield datafield="columnThree" headertext="Column 3"/> 
    </columns> 
</asp:GridView> 
+1

非常感謝你@ jadarnel27。仍然我不能相信問題的解決方案只是改變'假'=) – yiddow

+0

@yiddow有時它就是這麼簡單=) – jadarnel27