后台代码: protected void btnHistory_Click(object sender, EventArgs e)
{
HouseRentBusiness hrb=new HouseRentBusiness();
if (Request.QueryString["HouseID"] != null)
{
DataTable dt = new DataTable();
string message = hrb.selectHistoryDetail(Request.QueryString["HouseID"].ToString(),ref dt);
if (message == "")
{
GridView gvHistory = new GridView();
gvHistory.AutoGenerateColumns = false;
gvHistory.Width = Unit.Parse("100%");
gvHistory.AllowPaging = true;
gvHistory.PageSize = 2; BoundField bfHouseID = new BoundField(); bfHouseID.HeaderText = "单位编号"; bfHouseID.DataField = "HouseID";
gvHistory.Columns.Add(bfHouseID); gvHistory.DataSource = dt;
gvHistory.DataBind();
divGvHistory.Controls.Add(gvHistory);
}
}
}运行到gvHistory.DataBind();这句就报错,未将对象引用设置到对象实例。
如果 将 gvHistory.AllowPaging = true; gvHistory.PageSize = 2; 去掉,就不会报错。
或者 gvHistory.AllowPaging = true; gvHistory.PageSize = 2; 单数据源中的数据量小于gvHistory.PageSize 也不会报错请问这是什么原因?
{
HouseRentBusiness hrb=new HouseRentBusiness();
if (Request.QueryString["HouseID"] != null)
{
DataTable dt = new DataTable();
string message = hrb.selectHistoryDetail(Request.QueryString["HouseID"].ToString(),ref dt);
if (message == "")
{
GridView gvHistory = new GridView();
gvHistory.AutoGenerateColumns = false;
gvHistory.Width = Unit.Parse("100%");
gvHistory.AllowPaging = true;
gvHistory.PageSize = 2; BoundField bfHouseID = new BoundField(); bfHouseID.HeaderText = "单位编号"; bfHouseID.DataField = "HouseID";
gvHistory.Columns.Add(bfHouseID); gvHistory.DataSource = dt;
gvHistory.DataBind();
divGvHistory.Controls.Add(gvHistory);
}
}
}运行到gvHistory.DataBind();这句就报错,未将对象引用设置到对象实例。
如果 将 gvHistory.AllowPaging = true; gvHistory.PageSize = 2; 去掉,就不会报错。
或者 gvHistory.AllowPaging = true; gvHistory.PageSize = 2; 单数据源中的数据量小于gvHistory.PageSize 也不会报错请问这是什么原因?
去掉之后在页面上设置。 <asp:GridView ID="GridView1" runat="server" onrowcreated="GridView1_RowCreated"
AllowPaging="True" onrowediting="GridView1_RowEditing" PageSize="2">
</asp:GridView>后台在绑定
我的目的是GridView在cs文件里生成,以及其他一些针对GridView的操作。
在前台不放GridView控件