問題は、従業員フィールドの昇順または降順で並べ替える場合、次または前のボタンを押すと完全に並べ替えられ、昇順で並べ替えられ、昇順でのみ表示され、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);
}
}
}
}
}
}
}
しかし、ソートデータの上向き矢印と下向き矢印がヘッダー列に表示されていませんが、体を助けてください