GridView手动排序功能

 

前台代码:

<asp:GridView ID="GVData" runat="server" OnRowDataBound="GVData_RowDataBound" OnSorting="GVData_Sorting"
ShowFooter="true" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnRowCreated="GVData_RowCreated"
PageSize="15" CssClass="Grid">
<PagerSettings Mode="NumericFirstLast" Visible="False" />
<PagerStyle BackColor="LightSteelBlue" HorizontalAlign="Right" />
<HeaderStyle CssClass="GridHeader"></HeaderStyle>
<AlternatingRowStyle CssClass="GridAltItem" />
<Columns>
<asp:BoundField DataField="departmentName" HeaderText="科室" SortExpression="departmentName" />
<asp:BoundField DataField="appDate" HeaderText="日期" DataFormatString="{0:yyyy-MM-dd}" SortExpression="appDate"
ItemStyle-Width="35%"></asp:BoundField>
<asp:BoundField DataField="success" HeaderText="预约成功人数"></asp:BoundField>
<asp:BoundField DataField="cancelApp" HeaderText="取消预约人数"></asp:BoundField>
<asp:BoundField DataField="misApp" HeaderText="已失约人数"></asp:BoundField>
</Columns>
<RowStyle HorizontalAlign="Center" Height="25px" />
<EmptyDataTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="center" style="border-right: black 1px; border-top: black 1px; border-left: black 1px;
border-bottom: black 1px; background-color: whitesmoke;">
该列表中暂时无数据!
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>

OnRowCreated="GVData_RowCreated"  在列名后面加上箭头

protected void GVData_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)//如果是表头,则执行如下操作
{
foreach (TableCell tc in e.Row.Cells)
{
//这种方法直接新加一个控件,也可以换成图片
if (tc.Controls.Count > 0)//这里要判断一下此时是不是已经生成了linkbutton
{
string s1 = ((LinkButton)tc.Controls[0]).Text;
//样式Webdings是123都有自己对应的图片
((LinkButton)tc.Controls[0]).Text = s1.Replace(s1, s1 + "<font face='Webdings'>5</font>");
//倒序的样式
if (tc.Controls.Count > 0 && tc.Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
{
if (((LinkButton)tc.Controls[0]).CommandArgument.ToString() == ViewState["sortExpression"].ToString())
{
string s2 = ((LinkButton)tc.Controls[0]).Text;
if (ViewState["sortDirection"].ToString() == "Desc")
{
((LinkButton)tc.Controls[0]).Text = s2.Replace("5", "6");
}
}
}
}
}
}
}

后台代码:

//绑定数据的地方加上排序字段和方法
protected void InitGridView()
{
string sortdirection;
if (GridViewSortDirection == SortDirection.Ascending)
{
sortdirection = "asc";
}
else
{
sortdirection = "desc";
}

 

DateTime? t1 = null;
if (txtDate1.Text.Trim() != "")
t1 = DateTime.Parse(txtDate1.Text.Trim());

string strDept = ddlDepartment.SelectedValue == "" ? "" : ddlDepartment.SelectedItem.Text;
DataSet ds = null;
try
{
ds = BLL.GuaHaoData.Appointements.GetAppointmentsStatByDept(strDept,
t1,
Convert.ToDateTime(txtDate2.Text),
ddlType.SelectedItem.Text);
}

catch
{
}
DataView dv = new DataView(ds.Tables[0]);
dv.Sort = GridViewSortExpression + " " + sortdirection;
GVData.DataSource = dv;
GVData.DataBind();

LabPageSum.Text = Convert.ToString(GVData.PageCount);
LabCurrentPage.Text = Convert.ToString(((int)GVData.PageIndex + 1));
this.GoPage.Text = LabCurrentPage.Text.ToString();
}

protected void GVData_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
GridViewSortExpression = sortExpression; //存到ViewState中排序的字段:newsid

if (GridViewSortExpression == sortExpression)
{
if (GridViewSortDirection == SortDirection.Ascending) //设置排序方向
{
GridViewSortDirection = SortDirection.Descending;
}
else
{
GridViewSortDirection = SortDirection.Ascending;
}
}
this.InitGridView();
}

public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending; //默认升序
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}

public string GridViewSortExpression
{
get
{
if (ViewState["sortExpression"] == null)
ViewState["sortExpression"] = "appDate"; //默认排序字段
return ViewState["sortExpression"].ToString();
}
set { ViewState["sortExpression"] = value; }
}

posted @ 2013-01-15 09:48  solo~  阅读(285)  评论(0编辑  收藏  举报