An issue for Data binding at datagridview

751 Views Asked by At

Actually, say problem to that maybe wrong. I have 2 page. I can get the query string at second page from first page. And this query string is my critical part of query. When debug mode, i can see the result of query and they would be what i want. but it cannot be shown on my gridview. Here is my code block: my CustomerList page,

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 is as what i want but my bind part doesn't work, i cannot see the GridViewCustomerList when i run the project. I research something asp.net page life-cycle model, the target solution maybe related with that.

1

There are 1 best solutions below

2
On BEST ANSWER

The problem here is that you have no explicit <Column> declarations in your markup, and you have AutoGenerateColumns property set to "false". Thus, even though the data gets bound to your GridView control, it doesn't know what to display (so it displays nothing.

The easiest solution here is to just removed AutoGenerateColumns="False" from your GridView declaration (it is "true" by default), and you should be good to go. It will automatically create your columns based on your DataSource.

Alternately, you can specify which columns you want by adding a columns section to your 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>