用gridview排序的时候,在.NET里是不能被标注的,下面提供如下方法:主要是显示的时候进行了下判断,然后进行不同的排序,这样就给这个gridview加上了排序的箭头,有意思的是<font face='Webdings'>个样式,如果你对<font face='Webdings'>不是很了解,下面是提供的连接:
http://www.cnblogs.com/freeton/archive/2009/06/27/1512139.html
注意:
Aspx页面:
设置排序:SortExpression="zhiwei"(给相应的字段设置)
.cs
开始设置的默认的排序状态用ViewState进行设置,写在!IsPostBack下面。
主要用到:GVpositionrecods_RowCreated与GVpositionrecods_Sorting事件
页面
代码
<
asp:GridView AllowSorting
=
"
True
"
ID
=
"
GVpositionrecods
"
runat
=
"
server
"
PageSize
=
"
12
"
AllowPaging = " True " EmptyDataText = " 没有任何数据可以显示 " Width = " 99% " AutoGenerateColumns = " False "
OnRowDataBound = " GVpositionrecods_RowDataBound " OnSelectedIndexChanged = " GVpositionrecods_SelectedIndexChanged "
OnSelectedIndexChanging = " GVpositionrecods_SelectedIndexChanging " OnPageIndexChanging = " GVpositionrecods_PageIndexChanging "
OnRowCreated = " GVpositionrecods_RowCreated " OnSorting = " GVpositionrecods_Sorting " >
< Columns >
< asp:TemplateField HeaderText = " 职位 " SortExpression = " zhiwei " >
< ItemTemplate >
< a href = ' throwseeen2.aspx?qiyeid=<%#Eval("qiyeid")%>&id=<%#Eval("ORDER_FORM_ID") %> ' >
<% #Eval( " zhiwei " ) %>
</ a >
</ ItemTemplate >
< ItemStyle HorizontalAlign = " Center " />
</ asp:TemplateField >
< asp:TemplateField HeaderText = " 企业名称 " SortExpression = " qiyeid " >
< ItemTemplate >
< a href = ' seeenlist.aspx?getidforen=<%#Eval("qiyeid")%> ' >
<% #Eval( " qiye " ) %>
</ a >
</ ItemTemplate >
< ItemStyle HorizontalAlign = " Center " />
</ asp:TemplateField >
< asp:BoundField DataField = " COUNT " HeaderText = " 人数 " SortExpression = " COUNT " >
< ItemStyle HorizontalAlign = " Center " />
</ asp:BoundField >
< asp:BoundField DataField = " STAR_TIME " SortExpression = " STAR_TIME " HeaderText = " 发布时间 "
DataFormatString = " {0:yyyy-MM-dd} " HtmlEncode = " False " >
< ItemStyle HorizontalAlign = " Center " />
</ asp:BoundField >
< asp:BoundField DataField = " END_TIME " SortExpression = " END_TIME " HeaderText = " 结束时间 "
DataFormatString = " {0:yyyy-MM-dd} " HtmlEncode = " False " >
< ItemStyle HorizontalAlign = " Center " />
</ asp:BoundField >
< asp:BoundField DataField = " flag " HeaderText = " 状态 " >
< ItemStyle HorizontalAlign = " Center " />
</ asp:BoundField >
</ Columns >
< AlternatingRowStyle BackColor = " #E2F8FF " ></ AlternatingRowStyle >
< HeaderStyle HorizontalAlign = " Center " BackColor = " #99CCFF " ></ HeaderStyle >
< PagerStyle CssClass = " cssPager " />
</ asp:GridView >
AllowPaging = " True " EmptyDataText = " 没有任何数据可以显示 " Width = " 99% " AutoGenerateColumns = " False "
OnRowDataBound = " GVpositionrecods_RowDataBound " OnSelectedIndexChanged = " GVpositionrecods_SelectedIndexChanged "
OnSelectedIndexChanging = " GVpositionrecods_SelectedIndexChanging " OnPageIndexChanging = " GVpositionrecods_PageIndexChanging "
OnRowCreated = " GVpositionrecods_RowCreated " OnSorting = " GVpositionrecods_Sorting " >
< Columns >
< asp:TemplateField HeaderText = " 职位 " SortExpression = " zhiwei " >
< ItemTemplate >
< a href = ' throwseeen2.aspx?qiyeid=<%#Eval("qiyeid")%>&id=<%#Eval("ORDER_FORM_ID") %> ' >
<% #Eval( " zhiwei " ) %>
</ a >
</ ItemTemplate >
< ItemStyle HorizontalAlign = " Center " />
</ asp:TemplateField >
< asp:TemplateField HeaderText = " 企业名称 " SortExpression = " qiyeid " >
< ItemTemplate >
< a href = ' seeenlist.aspx?getidforen=<%#Eval("qiyeid")%> ' >
<% #Eval( " qiye " ) %>
</ a >
</ ItemTemplate >
< ItemStyle HorizontalAlign = " Center " />
</ asp:TemplateField >
< asp:BoundField DataField = " COUNT " HeaderText = " 人数 " SortExpression = " COUNT " >
< ItemStyle HorizontalAlign = " Center " />
</ asp:BoundField >
< asp:BoundField DataField = " STAR_TIME " SortExpression = " STAR_TIME " HeaderText = " 发布时间 "
DataFormatString = " {0:yyyy-MM-dd} " HtmlEncode = " False " >
< ItemStyle HorizontalAlign = " Center " />
</ asp:BoundField >
< asp:BoundField DataField = " END_TIME " SortExpression = " END_TIME " HeaderText = " 结束时间 "
DataFormatString = " {0:yyyy-MM-dd} " HtmlEncode = " False " >
< ItemStyle HorizontalAlign = " Center " />
</ asp:BoundField >
< asp:BoundField DataField = " flag " HeaderText = " 状态 " >
< ItemStyle HorizontalAlign = " Center " />
</ asp:BoundField >
</ Columns >
< AlternatingRowStyle BackColor = " #E2F8FF " ></ AlternatingRowStyle >
< HeaderStyle HorizontalAlign = " Center " BackColor = " #99CCFF " ></ HeaderStyle >
< PagerStyle CssClass = " cssPager " />
</ asp:GridView >
默认排序设置
代码
protected
void
Page_Load(
object
sender, EventArgs e)
{
if (Session[ " student " ] == null )
{
Response.Write( " <script>alert('页面已过期,请重新登录!');window.parent.frames.location.href='../Default.aspx'</script> " );
}
else if ( ! IsPostBack && Session[ " t_resume_info " ] != null )
{
ViewState[ " sortExp " ] = " STAR_TIME " ; // 默认排序的字段
ViewState[ " sortDir " ] = " Desc " ; // 字段是安什么排序的
// 绑定数据源
bind();
this .lbcontent.Visible = false ;
}
else if (Session[ " t_resume_info " ] == null )
{
this .lbcontent.Text = " 您还没有任何录取的信息 " ;
}
}
{
if (Session[ " student " ] == null )
{
Response.Write( " <script>alert('页面已过期,请重新登录!');window.parent.frames.location.href='../Default.aspx'</script> " );
}
else if ( ! IsPostBack && Session[ " t_resume_info " ] != null )
{
ViewState[ " sortExp " ] = " STAR_TIME " ; // 默认排序的字段
ViewState[ " sortDir " ] = " Desc " ; // 字段是安什么排序的
// 绑定数据源
bind();
this .lbcontent.Visible = false ;
}
else if (Session[ " t_resume_info " ] == null )
{
this .lbcontent.Text = " 您还没有任何录取的信息 " ;
}
}
绑定的数据源
代码
public
void
bind()
{
// 得到对应的简历的ID,从SN里面查看值
ccwu.Model.T_RESUME_INFO listTemp = (ccwu.Model.T_RESUME_INFO)Session[ " t_resume_info " ];
ccwu.Model.T_ORDER_RESUME torderresume = new ccwu.Model.T_ORDER_RESUME();
int id = Convert.ToInt32(listTemp.ID.ToString());
// 得到对应的简历的ID,从SN里面查看值
ccwu.DAL.T_JOB jobdal = new ccwu.DAL.T_JOB();
string where = " RESUME_INFO_ID=' " + id.ToString() + " ' " ;
DataSet ds = jobdal.getlistResume( where );
DataView dv = new DataView(ds.Tables[ 0 ]);
dv.Sort = ViewState[ " sortExp " ].ToString() + " " + ViewState[ " sortDir " ].ToString();
GVpositionrecods.DataSource = dv;
this .GVpositionrecods.DataBind();
if ( this .GVpositionrecods.Rows.Count > 0 )
{
}
else // 如果数据源是空的
{
// 表头的设置
GridViewRow row = new GridViewRow( - 1 , - 1 , DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
foreach (DataControlField field in this .GVpositionrecods.Columns)
{
TableCell cell = new TableCell();
cell.Text = field.HeaderText;
cell.Width = field.HeaderStyle.Width;
cell.Height = field.HeaderStyle.Height;
cell.ForeColor = field.HeaderStyle.ForeColor;
cell.Font.Size = field.HeaderStyle.Font.Size;
cell.Font.Bold = field.HeaderStyle.Font.Bold;
cell.Font.Name = field.HeaderStyle.Font.Name;
cell.Font.Strikeout = field.HeaderStyle.Font.Strikeout;
cell.Font.Underline = field.HeaderStyle.Font.Underline;
cell.BackColor = field.HeaderStyle.BackColor;
cell.VerticalAlign = field.HeaderStyle.VerticalAlign;
cell.HorizontalAlign = field.HeaderStyle.HorizontalAlign;
cell.CssClass = field.HeaderStyle.CssClass;
cell.BorderColor = field.HeaderStyle.BorderColor;
cell.BorderStyle = field.HeaderStyle.BorderStyle;
cell.BorderWidth = field.HeaderStyle.BorderWidth;
row.Cells.Add(cell);
}
TableItemStyle headStyle = this .GVpositionrecods.HeaderStyle;
TableItemStyle emptyStyle = GVpositionrecods.EmptyDataRowStyle;
emptyStyle.Width = headStyle.Width;
emptyStyle.Height = headStyle.Height;
emptyStyle.ForeColor = headStyle.ForeColor;
emptyStyle.Font.Size = headStyle.Font.Size;
emptyStyle.Font.Bold = headStyle.Font.Bold;
emptyStyle.Font.Name = headStyle.Font.Name;
emptyStyle.Font.Strikeout = headStyle.Font.Strikeout;
emptyStyle.Font.Underline = headStyle.Font.Underline;
emptyStyle.BackColor = headStyle.BackColor;
emptyStyle.VerticalAlign = headStyle.VerticalAlign;
emptyStyle.HorizontalAlign = headStyle.HorizontalAlign;
emptyStyle.CssClass = headStyle.CssClass;
emptyStyle.BorderColor = headStyle.BorderColor;
emptyStyle.BorderStyle = headStyle.BorderStyle;
emptyStyle.BorderWidth = headStyle.BorderWidth;
// 空白行的设置
GridViewRow row1 = new GridViewRow( 0 , - 1 , DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
TableCell cell1 = new TableCell();
cell1.Text = " 没有任何数据可以显示 " ;
cell1.BackColor = System.Drawing.Color.White;
row1.Cells.Add(cell1);
cell1.ColumnSpan = 6 ; // 合并列
if ( this .GVpositionrecods.Controls.Count == 0 )
{
GVpositionrecods.Page.Response.Write( " <script language='javascript'>alert('必须在初始化表格类之前执行DataBind方法并设置EmptyDataText属性不为空!');</script> " );
}
else
{
GVpositionrecods.Controls[ 0 ].Controls.Clear();
this .GVpositionrecods.Controls[ 0 ].Controls.AddAt( 0 , row);
this .GVpositionrecods.Controls[ 0 ].Controls.AddAt( 1 , row1);
}
}
}
{
// 得到对应的简历的ID,从SN里面查看值
ccwu.Model.T_RESUME_INFO listTemp = (ccwu.Model.T_RESUME_INFO)Session[ " t_resume_info " ];
ccwu.Model.T_ORDER_RESUME torderresume = new ccwu.Model.T_ORDER_RESUME();
int id = Convert.ToInt32(listTemp.ID.ToString());
// 得到对应的简历的ID,从SN里面查看值
ccwu.DAL.T_JOB jobdal = new ccwu.DAL.T_JOB();
string where = " RESUME_INFO_ID=' " + id.ToString() + " ' " ;
DataSet ds = jobdal.getlistResume( where );
DataView dv = new DataView(ds.Tables[ 0 ]);
dv.Sort = ViewState[ " sortExp " ].ToString() + " " + ViewState[ " sortDir " ].ToString();
GVpositionrecods.DataSource = dv;
this .GVpositionrecods.DataBind();
if ( this .GVpositionrecods.Rows.Count > 0 )
{
}
else // 如果数据源是空的
{
// 表头的设置
GridViewRow row = new GridViewRow( - 1 , - 1 , DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
foreach (DataControlField field in this .GVpositionrecods.Columns)
{
TableCell cell = new TableCell();
cell.Text = field.HeaderText;
cell.Width = field.HeaderStyle.Width;
cell.Height = field.HeaderStyle.Height;
cell.ForeColor = field.HeaderStyle.ForeColor;
cell.Font.Size = field.HeaderStyle.Font.Size;
cell.Font.Bold = field.HeaderStyle.Font.Bold;
cell.Font.Name = field.HeaderStyle.Font.Name;
cell.Font.Strikeout = field.HeaderStyle.Font.Strikeout;
cell.Font.Underline = field.HeaderStyle.Font.Underline;
cell.BackColor = field.HeaderStyle.BackColor;
cell.VerticalAlign = field.HeaderStyle.VerticalAlign;
cell.HorizontalAlign = field.HeaderStyle.HorizontalAlign;
cell.CssClass = field.HeaderStyle.CssClass;
cell.BorderColor = field.HeaderStyle.BorderColor;
cell.BorderStyle = field.HeaderStyle.BorderStyle;
cell.BorderWidth = field.HeaderStyle.BorderWidth;
row.Cells.Add(cell);
}
TableItemStyle headStyle = this .GVpositionrecods.HeaderStyle;
TableItemStyle emptyStyle = GVpositionrecods.EmptyDataRowStyle;
emptyStyle.Width = headStyle.Width;
emptyStyle.Height = headStyle.Height;
emptyStyle.ForeColor = headStyle.ForeColor;
emptyStyle.Font.Size = headStyle.Font.Size;
emptyStyle.Font.Bold = headStyle.Font.Bold;
emptyStyle.Font.Name = headStyle.Font.Name;
emptyStyle.Font.Strikeout = headStyle.Font.Strikeout;
emptyStyle.Font.Underline = headStyle.Font.Underline;
emptyStyle.BackColor = headStyle.BackColor;
emptyStyle.VerticalAlign = headStyle.VerticalAlign;
emptyStyle.HorizontalAlign = headStyle.HorizontalAlign;
emptyStyle.CssClass = headStyle.CssClass;
emptyStyle.BorderColor = headStyle.BorderColor;
emptyStyle.BorderStyle = headStyle.BorderStyle;
emptyStyle.BorderWidth = headStyle.BorderWidth;
// 空白行的设置
GridViewRow row1 = new GridViewRow( 0 , - 1 , DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
TableCell cell1 = new TableCell();
cell1.Text = " 没有任何数据可以显示 " ;
cell1.BackColor = System.Drawing.Color.White;
row1.Cells.Add(cell1);
cell1.ColumnSpan = 6 ; // 合并列
if ( this .GVpositionrecods.Controls.Count == 0 )
{
GVpositionrecods.Page.Response.Write( " <script language='javascript'>alert('必须在初始化表格类之前执行DataBind方法并设置EmptyDataText属性不为空!');</script> " );
}
else
{
GVpositionrecods.Controls[ 0 ].Controls.Clear();
this .GVpositionrecods.Controls[ 0 ].Controls.AddAt( 0 , row);
this .GVpositionrecods.Controls[ 0 ].Controls.AddAt( 1 , row1);
}
}
}
关键代码
代码
protected
void
GVpositionrecods_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[ " sortExp " ].ToString())
{
string s2 = ((LinkButton)tc.Controls[ 0 ]).Text;
if (ViewState[ " sortDir " ].ToString() == " Desc " )
{
((LinkButton)tc.Controls[ 0 ]).Text = s2.Replace( " 5 " , " 6 " );
}
}
}
}
}
}
}
{
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[ " sortExp " ].ToString())
{
string s2 = ((LinkButton)tc.Controls[ 0 ]).Text;
if (ViewState[ " sortDir " ].ToString() == " Desc " )
{
((LinkButton)tc.Controls[ 0 ]).Text = s2.Replace( " 5 " , " 6 " );
}
}
}
}
}
}
}
当点击列头的时候
代码
protected
void
GVpositionrecods_Sorting(
object
sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;
if (ViewState[ " sortExp " ].ToString() == sPage)
{
if (ViewState[ " sortDir " ].ToString() == " Desc " )
ViewState[ " sortDir " ] = " ASC " ;
else
ViewState[ " sortDir " ] = " Desc " ;
}
else
{
ViewState[ " sortExp " ] = e.SortExpression;
}
bind();
}
{
string sPage = e.SortExpression;
if (ViewState[ " sortExp " ].ToString() == sPage)
{
if (ViewState[ " sortDir " ].ToString() == " Desc " )
ViewState[ " sortDir " ] = " ASC " ;
else
ViewState[ " sortDir " ] = " Desc " ;
}
else
{
ViewState[ " sortExp " ] = e.SortExpression;
}
bind();
}