gridview 实现分页后发现数据丢失。(第二页点了之后没数据) 求指导,谢谢了!
我的代码:a.aspx<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKe...
我的代码:
a.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="cid" CellPadding="4" ForeColor="#333333" GridLines="None"
AllowPaging="true" PageSize="8"
OnPageIndexChanging="GridView1_PageIndexChanging" Width="449px" >
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="cid" HeaderText="课程号" ReadOnly="True"
SortExpression="cid" />
<asp:BoundField DataField="cname" HeaderText="课程名" SortExpression="cname" />
<asp:BoundField DataField="credit" HeaderText="学分"
SortExpression="credit" />
<asp:BoundField DataField="hour" HeaderText="课时" SortExpression="hour" />
<asp:BoundField DataField="tid" HeaderText="教师名" SortExpression="" />
<asp:TemplateField><ItemTemplate>
</ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
a.aspx.cs 中:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string idk = "95001";
string sqlconn;
sqlconn = @"Server=localhost;Initial Catalog=DBproject;Integrated Security=True;User ID=lin1;Password=1213";
SqlConnection conn = new SqlConnection(sqlconn);
conn.Open();
// string select = "select * from courses where cid not in (select cid from choices where sid='" + idk + "') and cid in (select distinct cid from tcourses where ft in ('Y','y'))";
string select = "select courses.* ,tid from courses,tcourses where courses.cid =tcourses.cid and courses.cid not in (select cid from choices where sid='" + idk + "' )and courses.cid in (select distinct cid from tcourses where ft in ('Y','y'))";
SqlDataAdapter da = new SqlDataAdapter(select , conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
DataBind();//重新绑定一遍数据
} 展开
a.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="cid" CellPadding="4" ForeColor="#333333" GridLines="None"
AllowPaging="true" PageSize="8"
OnPageIndexChanging="GridView1_PageIndexChanging" Width="449px" >
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="cid" HeaderText="课程号" ReadOnly="True"
SortExpression="cid" />
<asp:BoundField DataField="cname" HeaderText="课程名" SortExpression="cname" />
<asp:BoundField DataField="credit" HeaderText="学分"
SortExpression="credit" />
<asp:BoundField DataField="hour" HeaderText="课时" SortExpression="hour" />
<asp:BoundField DataField="tid" HeaderText="教师名" SortExpression="" />
<asp:TemplateField><ItemTemplate>
</ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
a.aspx.cs 中:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string idk = "95001";
string sqlconn;
sqlconn = @"Server=localhost;Initial Catalog=DBproject;Integrated Security=True;User ID=lin1;Password=1213";
SqlConnection conn = new SqlConnection(sqlconn);
conn.Open();
// string select = "select * from courses where cid not in (select cid from choices where sid='" + idk + "') and cid in (select distinct cid from tcourses where ft in ('Y','y'))";
string select = "select courses.* ,tid from courses,tcourses where courses.cid =tcourses.cid and courses.cid not in (select cid from choices where sid='" + idk + "' )and courses.cid in (select distinct cid from tcourses where ft in ('Y','y'))";
SqlDataAdapter da = new SqlDataAdapter(select , conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
DataBind();//重新绑定一遍数据
} 展开
2个回答
展开全部
这里你不是用的自带的那个DataSource控件,所以这里直接用DataBind()重新绑定时没用的吧!你在GridView1_PageIndexChanging()方法里面应该把他的DataSource重设一次,然后再DataBind绑定一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你从数据库取出来的结果 有多少条?会不会只有PageSize="8" 8条
追问
有多条。。多于19条。。。。
追答
你上面的代码没问题 看看你的
DataBind();//重新绑定一遍数据
这个函数吧
DataBind();这是系统的函数 你重载它了???
还是你认为用DataBind();实现绑定数据
你GridView1.PageIndex = e.NewPageIndex;
后必须在绑定一遍string select = "select courses.* ,tid from courses,tcourses where courses.cid =tcourses.cid and courses.cid not in (select cid from choices where sid='" + idk + "' )and courses.cid in (select distinct cid from tcourses where ft in ('Y','y'))";
SqlDataAdapter da = new SqlDataAdapter(select , conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询