Visual studio 2010 GridView将消失,不会出现任何错误
我已将Gridview设置为允许分页Visual studio 2010 GridView将消失,不会出现任何错误,visual-studio-2010,sql-server-2008,gridview,Visual Studio 2010,Sql Server 2008,Gridview,我已将Gridview设置为允许分页 try { SqlConnection sqlConnection = new SqlConnection("Data Source=JACKCONNECTION\\SQLEXPRESS;Initial Catalog=testbase;Integrated Security=True"); SqlCommand sqlCommand = new SqlCommand(alli
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();
}
<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>
最后,包含Gridview的aspx文件如下所示:
protected void gvm_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewMass.PageIndex = e.NewPageIndex;
GridViewMass.DataBind();
}
<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页还是我可以点击的任何页面),GridviewGridViewMass
就会消失
我编码错了吗?以前,我遇到了以下错误消息,并已解决了这些问题,但现在,我遇到了一些无法继续的问题
感谢任何能帮助我找回GridView的帮助 您在
pageIndexchange
事件中只缺少一件事
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;
}
现在在您的pageindexchange
中,像这样更改
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();
}
还记得将
启用排序和分页回调设置为False
您还创建了一个方法fillGrid(),在PageIndexchange中,您只需调用该方法即可。我希望接受您的答案,但您的代码不起作用。我甚至尝试更改ViewState[“datasource”]=ds.Copy()
toViewState[“DataSource”]=ds.Copy()代码>和GridViewMass.DataSource=(数据集)(ViewState[“数据])代码>到GridViewMass.DataSource=(数据集)(ViewState[“数据源])
但仍然无法工作。我认识到您需要将启用排序和分页回调设置为False,并将您的答案设置为False,以使其正常工作,因此我更新了您的答案并将其标记为正确。你拿到15分了吗?