我已将Gridview设置为允许分页。
try
{
SqlConnection sqlConnection = new SqlConnection("Data Source=JACKCONNECTION\\SQLEXPRESS;Initial Catalog=testbase;Integrated Security=True");
SqlCommand sqlCommand = new SqlCommand(allitemsselectedsqlsrc, sqlConnection);
sqlCommand.CommandType = System.Data.CommandType.Text;
sqlConnection.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
DataSet ds = new DataSet();
da.Fill(ds);
ArrayList ArrList = new ArrayList();
foreach (DataRow dr in ds.Tables[0].Rows)
{
ArrList.Add(dr);
}
GridViewMass.DataSource = ds;
GridViewMass.DataBind();
}
catch (Exception err)
{
LabelSelErr.Text = err.Message;
}
另外,我对GridView有一个动作PageIndexChanging
,如下所示:
protected void gvm_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewMass.PageIndex = e.NewPageIndex;
GridViewMass.DataBind();
}
最后,包含Gridview的aspx文件如下:
<asp:GridView ID="GridViewMass" runat="server" AllowPaging="True"
AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" EnableSortingAndPagingCallbacks="True"
ForeColor="Black" GridLines="Horizontal"
onpageindexchanging="gvm_PageIndexChanging">
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
奇怪的是,当我点击页面(无论是第2页,第3页还是我可以点击的任何页面)时,Gridview GridViewMass
将会消失。
我的代码是错误的吗?以前,我遇到了以下错误消息并解决了它们,但是现在,我最终得到的东西不能再继续了。
感谢任何帮助我恢复GridView的帮助。
答案 0 :(得分:1)
您在PageIndexChanging
活动
protected void gvm_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Here you missing to give datasource to your Grid...
GridViewMass.PageIndex = e.NewPageIndex;
GridViewMass.DataBind();
}
在您的Try / catch块中,您必须将该数据集存储在ViewState
try
{
SqlConnection sqlConnection = new SqlConnection("Data Source=JACKCONNECTION\\SQLEXPRESS;Initial Catalog=testbase;Integrated Security=True");
SqlCommand sqlCommand = new SqlCommand(allitemsselectedsqlsrc, sqlConnection);
sqlCommand.CommandType = System.Data.CommandType.Text;
sqlConnection.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
DataSet ds = new DataSet();
da.Fill(ds);
ArrayList ArrList = new ArrayList();
foreach (DataRow dr in ds.Tables[0].Rows)
{
ArrList.Add(dr);
}
ViewState["DataSource"] = ds;
GridViewMass.DataSource = ds;
GridViewMass.DataBind();
}
catch (Exception err)
{
LabelSelErr.Text = err.Message;
}
现在进行pageIndexChangeing
更改
protected void gvm_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Here you missing to give datasource to your Grid...
GridViewMass.DataSource = (DataSet)(ViewState["DataSource"]);
GridViewMass.PageIndex = e.NewPageIndex;
GridViewMass.DataBind();
}
另请记住将EnableSortingAndPagingCallbacks
设置为False
!