问题是,如果我使用员工字段进行排序或者取消发送它是完美排序如果按下下一个或上一个按钮它正在排序集合顺序并且仅按照排序顺序显示任何正文可以帮助tp如何在GridView1_PageIndexChanging中绑定gridview数据( )活动完美无缺
这是我的default3.aspx页面
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="true"
AutoGenerateColumns="False" onsorting="GridView1_Sorting"
CurrentSortField="employeeid" CurrentSortDirection="ASC"
onrowcreated="GridView1_RowCreated" AllowPaging="true"
CaptionAlign="Bottom" onpageindexchanging="GridView1_PageIndexChanging"
onprerender="GridView1_PreRender"
PageSize="2">
<Columns>
<asp:BoundField DataField="EmployeeId" HeaderText="Last Name"
ItemStyle-Width="15%" SortExpression="EmployeeId" >
<ItemStyle Width="15%"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="First Name" ItemStyle-Width="15%"
SortExpression="Name" >
<ItemStyle Width="15%"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="gender" HeaderText="Email" ItemStyle-Width="15%" >
<ItemStyle Width="15%"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Change Password" ItemStyle-Width="15%">
<ItemTemplate>
<asp:LinkButton ID="imgbtn1" runat="server">change password</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="15%"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="city" HeaderText="Date created" ItemStyle-Width="15%">
<ItemStyle Width="15%"></ItemStyle>
</asp:BoundField>
</Columns>
<PagerSettings Mode="NextPreviousFirstLast" />
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/images/up_arrow.png" Width="10px" />
</form>
</body>
</html>
这是我的default.aspx.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string gender { get; set; }
public string city { get; set; }
}
public static DataSet getallemployees()
{
String cs = "Data Source=.;database=users;Integrated Security=SSPI";
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("select * from tblEmployees", con);
SqlDataAdapter da = new SqlDataAdapter("select * from tblEmployees", con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
public static List<Employee> GetAllEmployees1(string sortColumn)
{
List<Employee> listEmployees = new List<Employee>();
String cs = "Data Source=.;database=users;Integrated Security=SSPI";
using (SqlConnection con = new SqlConnection(cs))
{
string sqlQuery = "select * from tblEmployees";
if (!string.IsNullOrEmpty(sortColumn))
{
sqlQuery += " order by " + sortColumn;
}
con.Open();
SqlCommand cmd = new SqlCommand(sqlQuery, con);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.EmployeeId = Convert.ToInt32(rdr["Employeeid"]);
employee.Name = rdr["name"].ToString();
employee.Name = rdr["gender"].ToString();
employee.Name = rdr["city"].ToString();
listEmployees.Add(employee);
}
rdr.Close();
}
return listEmployees;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource= GetAllEmployees1("employeeid");
GridView1.DataBind();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
SortDirection sortDirection = SortDirection.Ascending;
string sortField = string.Empty;
SortGridview(GridView1, e, out sortDirection, out sortField);
string strsortDirection = sortDirection == SortDirection.Ascending ? "ASC" : "DESC";
GridView1.DataSource = GetAllEmployees1(e.SortExpression + " " + strsortDirection);
GridView1.DataBind();
}
private void SortGridview(GridView gridview, GridViewSortEventArgs e, out SortDirection sortDirection, out string sortField)
{
sortField = e.SortExpression;
sortDirection = e.SortDirection;
if (gridview.Attributes["CurrentSortField"] != null && gridview.Attributes["CurrentSortDirection"] != null)
{
if (sortField == gridview.Attributes["CurrentSortField"])
{
if (gridview.Attributes["CurrentSortDirection"] == "ASC")
{
sortDirection = SortDirection.Descending;
}
else
{
sortDirection = SortDirection.Ascending;
}
}
gridview.Attributes["CurrentSortField"] = sortField;
gridview.Attributes["CurrentSortDirection"] = (sortDirection == SortDirection.Ascending ? "ASC" : "DESC");
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (GridView1.Attributes["CurrentSortField"] != null && GridView1.Attributes["CurrentSortDirection"] != null)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell tableCell in e.Row.Cells)
{
if (tableCell.HasControls())
{
LinkButton sortLinkButton = null;
if (tableCell.Controls[0] is LinkButton)
{
sortLinkButton = (LinkButton)tableCell.Controls[0];
}
if (sortLinkButton != null && GridView1.Attributes["CurrentSortField"] == sortLinkButton.CommandArgument)
{
Image image = new Image();
if (GridView1.Attributes["CurrentSortDirection"] == "ASC")
{
image.ImageUrl = "~/images/down_arrow.png";
image.Width = 10;
image.Height = 10;
}
else
{
image.ImageUrl = "~/images/~/images/up_arrow.png";
image.Width = 10;
image.Height = 10;
}
tableCell.Controls.Add(new LiteralControl(" "));
tableCell.Controls.Add(image);
}
}
}
}
}
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
Label1.Text = "Displaying Page " + (GridView1.PageIndex + 1).ToString() + " of " + GridView1.PageCount.ToString();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = getallemployees();
GridView1.DataBind();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class viewstate_dataset : System.Web.UI.Page
{
public DataSet getallemployees
{
get{
if (ViewState["Empdetails"] == null)
{
String cs = "Data Source=.;database=users;Integrated Security=SSPI";
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("select * from tblEmployees", con);
SqlDataAdapter da = new SqlDataAdapter("select EmployeeId,Name,gender+','+Name as gender,city from tblEmployees", con);
DataSet ds = new DataSet();
da.Fill(ds);
ViewState["Empdetails"] = ds;
}
} return (DataSet)ViewState["Empdetails"];
}
set
{
ViewState["Empdetails"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = getallemployees;
GridView1.DataBind();
Session["sortDirection"] = SortDirection.Descending;
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sortexp=e.SortExpression;
Session["sortexp"] = sortexp;
if (Session["sortDirection"] != null && Session["sortDirection"].ToString() == SortDirection.Descending.ToString())
{
Session["sortDirection"] = SortDirection.Ascending;
sort (sortexp, "ASC");
}
else
{
Session["sortDirection"] = SortDirection.Descending;
sort(sortexp, "DESC");
}
}
private void sort(string soreExpression, string p)
{
DataView dv = null;
dv = new DataView(getallemployees.Tables[0]);
dv.Sort = soreExpression + " " + p;
GridView1.DataSource = dv;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string sortExp = string.Empty;
string NewSortDirection = string.Empty;
GridView1.PageIndex = e.NewPageIndex;
if (Session["sortexp"] != null)
{
sortExp = (string)Session["sortexp"];
if (Session["sortDirection"] != null && Session["sortDirection"].ToString() == SortDirection.Ascending.ToString())
{
NewSortDirection = "ASC";
}
else
{
NewSortDirection = "DESC";
}
sort(sortExp, NewSortDirection);
}
else
{
GridView1.DataSource = getallemployees;
GridView1.DataBind();
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (GridView1.Attributes["sortexp"] != null && Session["sortDirection"] != null)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell tableCell in e.Row.Cells)
{
if (tableCell.HasControls())
{
LinkButton sortLinkButton = null;
if (tableCell.Controls[0] is LinkButton)
{
sortLinkButton = (LinkButton)tableCell.Controls[0];
}
if (sortLinkButton != null && Session["sortexp"].ToString() == sortLinkButton.CommandArgument)
{
Image image = new Image();
if (Session["sortDirection"].ToString() == "ASC")
{
image.ImageUrl = "~/images/down_arrow.png";
image.Width = 10;
image.Height = 10;
}
else
{
image.ImageUrl = "~/images/up_arrow.png";
image.Width = 10;
image.Height = 10;
}
tableCell.Controls.Add(new LiteralControl(" "));
tableCell.Controls.Add(image);
}
}
}
}
}
}
}
但是当排序数据向上和向下箭头没有出现在标题列上时,任何正文帮助我
答案 0 :(得分:0)
问题是您在getallemployees()
函数上调用了GridView1_PageIndexChanging
哪个函数正在为下一页返回未分类的数据。
要解决此问题,您需要使用ViewState
来保持数据的持久性。
以下解决方案将极大地帮助您sorting your grid using ViewState
http://www.stackoverflow.com/questions/702600/sorting-and-paging-with-gridview-asp-net?rq=1