几天来一直试图解决这个gridview问题。
我得到了 2 个不同的网格视图,它们具有不同的信息,可以通过单击标题进行排序。还有一个分页功能来组织信息。除此之外,我还有一个搜索按钮,我可以在其中搜索网格视图中存在的所有信息。但是,仅对于 page_load 网格视图,分页/排序功能起作用,但搜索到的网格视图无法分页/排序。
我将向您展示我如何为一个默认的 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();
}
然后我将gridview绑定在另一种称为datatable的方法下
private 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();
}
正如您从我的搜索按钮中看到的那样,我重新绑定了我的整个网格视图,我相信它应该再次调用排序/页面代码,但不幸的是它没有。事实上,当我尝试排序/分页时,它会显示出显示的 page_load 信息。如果有人能告诉我如何在搜索尝试完成时为搜索到的 gridview 启用分页/排序,我将不胜感激。
问候。