我想使用页面加载时绑定数据,写   
 protected void Page_Load(object sender, EventArgs e)
    {
       this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL();
    }
但是运行以后页面上没有数据,什么都不显示于是我用手工的方法设计GridView1
以下是源代码:         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" Height="287px" Width="765px" DataSourceID="ObjectDataSource1" AllowPaging="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" GridLines="Vertical" OnRowDeleted="GridView1_RowDeleted" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="DeptID" HeaderText="部门ID" SortExpression="DeptID" />
                <asp:BoundField DataField="DeptName" HeaderText="部门名称" SortExpression="DeptName" />
                <asp:BoundField DataField="Personnel" HeaderText="人员编制" SortExpression="Personnel" />
                <asp:BoundField DataField="Governor" HeaderText="部门总监" SortExpression="Governor" />
                <asp:BoundField DataField="Budget" HeaderText="经费预算" SortExpression="Budget" />
                <asp:BoundField DataField="DeptDsc" HeaderText="部门详细描述" SortExpression="DeptDsc" />
            </Columns>
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
             <AlternatingRowStyle BackColor="Gainsboro" />
        </asp:GridView>
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get_Dept_ALL"
                TypeName="BLL.DeptBLL"></asp:ObjectDataSource>运行就是显示出来了,不知道是为什么,希望大家帮解答下,我刚学。

解决方案 »

  1.   

     this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL(); 
    this.GridView1.Databind();少这句
      

  2.   

    既然用到了ObjectDataSource,那么Page_Load里的this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL(); 完全没必要。
      

  3.   

    没绑定
    加这个this.GridView1.Databind();就好了
      

  4.   

    protected void Page_Load(object sender, EventArgs e) 
        { 
          this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL(); 
          this.GridView1.DataBind
        } 
    ();//web程序控件的数据绑定和winform程序的控件数据绑定要注意这个区别,控件指定数据源后一定要调用绑定方法
      

  5.   

    少了databind,数据要绑定哦
      

  6.   

    对,是少了this.GridView1.DataBind();这句 
      

  7.   

    是少了databind哦..
    不過既然用到了ObjectDataSource,那么Page_Load里的this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL(); 完全没必要。
      

  8.   

    protected void Page_Load(object sender, EventArgs e) 
        { 
          this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL(); 
          GridView1.DataBind();
        } 
      

  9.   

    加上 this.GridView1.DataBind();调用databind 方法后 gridview 才能显示,不过前提是你的数据库里面有数据,要是没有的话,照样是不显示的!
      

  10.   

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" Height="287px" Width="765px" DataSourceID="ObjectDataSource1" 都设置DataSourceID是你的ObjectDataSource1了.你还写那代码干啥?
      

  11.   


    If(!IsPostBack)//这个不要忘了
    {
          this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL();
          this.GridView1.DataBind 
    }
      

  12.   

    请检查 Get_Dept_ALL 代码是否正确。
    跟踪看看这个方法是不是有返回值。
      

  13.   

    注意,this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL(); 
    this.GridView1.Databind();
    最好能写在ispostBack里面,如下:
    if(!isPostBack)
    {
      this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL(); 
      this.GridView1.Databind();
    }要不然后面做事件时很容易报错