几天来一直试图解决这个gridview问题。
我有2个不同的网格视图,其中包含不同的信息,可以通过单击标题进行排序。还有一个分页功能来组织信息。除此之外,我还有一个搜索按钮,我可以在其中搜索gridview中的所有信息。但是,仅对于page_load gridview,页面/排序功能有效,但搜索到的gridview无法进行分页/排序。
我将向您展示我如何为单个默认网格视图执行页面/排序功能。
首先,我需要将这些gridview绑定在这样的数据集下,并在页面加载时显示信息
Session["gridview"] = DataBindByDataSet();
GVPolice.DataSource = Session["gridview"];
GVPolice.DataBind();
using (var connAdd = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
connAdd.Open();
var sql = "select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC], address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available'";
using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
{
DataSet dsSel = new DataSet();
cmdAdd.Fill(dsSel);
GVPolice.DataSource = dsSel;
GVPolice.DataBind();
}
然后我在另一个名为datatable
的方法下绑定gridviewprivate DataTable DataBindByDataSet()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC], address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available'", conn);
da.Fill(ds);
conn.Close();
return ds.Tables[0];
}
下面基本上是如何排序页面加载时显示的数据
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = Session["gridview"] as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
// dt.DefaultView.Sort = e.SortExpression.ToString();
this.GVPolice.DataSource = Session["gridview"];
GVPolice.DataBind();
}
}
private string GetSortDirection(string column)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
这是分页明智的
protected void GVPolice_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GVPolice.PageIndex = e.NewPageIndex;
GVPolice.DataBind();
}
这就是我搜索信息的方式
protected void btnSearch_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC], address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available' and " + ddlCategory.SelectedItem.Text + " like '%" + txtData.Text + "%'", conn);
da.Fill(ds);
GVPolice.DataSource = ds.Copy();
GVPolice.DataBind();
conn.Close();
}
从我的搜索按钮中可以看到,我重新绑定了我的整个gridview,我相信它应该再次调用sort / page代码,但不幸的是它没有。事实上,当我尝试排序/分页时,它将显示已显示的page_load信息。如果有人能够在搜索尝试完成后如何启用页面/排序搜索的gridview,我将不胜感激。
问候。
答案 0 :(得分:2)
确保已将页面加载代码放在if块中,如下所示
if (!IsPostBack)
{
Session["gridview"] = DataBindByDataSet();
GVPolice.DataSource = Session["gridview"];
GVPolice.DataBind();
using (var connAdd = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
connAdd.Open();
var sql = "select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC], address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available'";
using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
{
DataSet dsSel = new DataSet();
cmdAdd.Fill(dsSel);
GVPolice.DataSource = dsSel;
GVPolice.DataBind();
}
.....
.....
}
}
并在btnSearch_Click
事件中将过滤后的数据存储在会话中,因为您使用Session["gridview"]
对GridView1_Sorting
事件中的数据进行排序。
protected void btnSearch_Click(object sender, EventArgs e)
{
.......
.......
GVPolice.DataSource = ds.Copy();
GVPolice.DataBind();
Session["gridview"] = ds.Tables[0];
conn.Close()
}
现在我们可以对过滤后的数据进行排序。
为了使分页能够正常使用过滤数据,我们需要更改GVPolice_PageIndexChanging
事件,如下所示。
protected void GVPolice_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GVPolice.PageIndex = e.NewPageIndex;
GVPolice.DataSource = Session["gridview"];
GVPolice.DataBind();
}