急求:如何在GridView的排序时显示“向上”“向下”箭头的图片。在线。[100分]
就是这种效果
-------------------------------
学号|姓名|数学成绩 ↑|语言成绩 ↓
-------------------------------
1 |小王 | 98 | 60
----------------------------
2 |小刘 | 80 | 90
-------------------------
当点击GridView列名时,会引发排序的,但不知道如何在列名上加一个指上指下的箭头(图片)。请高手说说,如何给列名上加图片呢
[解决办法]
Private Shared SortStr, SortDirection As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Try
binddata() '你的code
Catch ex As Exception
Response.Redirect( "Error.aspx ")
End Try
SortStr = "UserID " 'your key
SortDirection = " Asc "
End If
End Sub
Private Sub binddata()
Dim MyView As DataView
MyView = ' ' ' ' 'your dataview
MyView.Sort = SortStr & SortDirection
dagUsers.DataSource = MyView
dagUsers.DataBind()
End Sub
Private Sub dagUsers_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dagUsers.SortCommand 'yourdatagrid event
If SortStr = e.SortExpression Then
If Trim(SortDirection) = "Asc " Then
SortDirection = " Desc "
Else
SortDirection = " Asc "
End If
Else
SortStr = e.SortExpression
End If
ResetColumn()
Select Case e.SortExpression
Case "UserID " 'your column
SetArrow(SortDirection, 0, dagUsers)
Case "LastName " 'your column
SetArrow(SortDirection, 1, dagUsers)
Case "FirstName " 'your column
SetArrow(SortDirection, 2, dagUsers)
Case "IP " 'your column
SetArrow(SortDirection, 3, dagUsers)
End Select
binddata()
End Sub
Private Sub ResetColumn()
dagUsers.Columns(0).HeaderText = "UserID " 'your column
dagUsers.Columns(1).HeaderText = "LastName " 'your column
dagUsers.Columns(2).HeaderText = "FirstName " 'your column
dagUsers.Columns(3).HeaderText = "IP " 'your column
End Sub
Public Sub SetArrow(ByVal Direction As String, ByVal ColumnIndex As Integer, ByRef SortGrid As DataGrid)
If Trim(Direction) = "Asc " Then
SortGrid.Columns(ColumnIndex).HeaderText = SortGrid.Columns(ColumnIndex).HeaderText & " <img src= 'Images/ArrowUp.jpg ' border= '0 '> "
Else
SortGrid.Columns(ColumnIndex).HeaderText = SortGrid.Columns(ColumnIndex).HeaderText & " <img src= 'Images/ArrowDown.jpg ' border= '0 '> "
End If
End Sub
------解决方案--------------------
GridView.aspx
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "GridView.aspx.cs " Inherits= "GridView " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div >
<asp:GridView ID= "GridView1 " runat= "server " AllowPaging= "True " AllowSorting= "True "
AutoGenerateColumns= "False " DataKeyNames= "CustomerID " DataSourceID= "SqlDataSource1 "
OnRowCreated= "GridView1_RowCreated " OnSorting= "GridView1_Sorting " PageSize= "5 ">
<Columns>
<asp:BoundField DataField= "CompanyName " HeaderText= "公司名称 " SortExpression= "CompanyName " />
<asp:BoundField DataField= "ContactName " HeaderText= "联系人 " SortExpression= "ContactName " />
<asp:BoundField DataField= "Address " HeaderText= "地址 " SortExpression= "Address " />
<asp:BoundField DataField= "City " HeaderText= "城市 " SortExpression= "City " />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ConnectionString= " <%$ ConnectionStrings:NorthwindConnectionString %> "
SelectCommand= "SELECT * FROM [Customers] "> </asp:SqlDataSource>
<asp:Label ID= "Label1 " runat= "server " Text= "Label "> </asp:Label> <br />
<br />
d </div>
</form>
</body>
</html>
GridView.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridView : System.Web.UI.Page
{
string sortexp = " ";//排序表达式
string sortdir = " ";//排序方向
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell tc in e.Row.Cells)
{
//这种方法直接新加一个控件,也可以换成图片
//Label ls = new Label();
//ls.Font.Name = "Webdings ";
//ls.Text = "5 ";
//tc.Controls.Add(ls);
if (tc.Controls.Count > 0 )//这里要判断一下此时是不是已经生成了linkbutton
{ string s1 = ((LinkButton)tc.Controls[0]).Text;
((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==sortexp)
{
//Label l = new Label();
//l.Font.Name = "Webdings ";
//if (sortdir == "Ascending ")
//{
// l.Text = "5 ";
//}
//else
//{
// l.Text = "6 ";
//}
//tc.Controls.Remove(ls);
//tc.Controls.Add(l);
string s2 = ((LinkButton)tc.Controls[0]).Text;
if (sortdir== "Descending ")
{
((LinkButton)tc.Controls[0]).Text = s2.Replace( "5 ", "6 ");
}
}
}
}
}
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
sortexp = e.SortExpression.ToString();
sortdir = e.SortDirection.ToString();
}
}
[解决办法]
我都是这样的,从word里拷一个箭头出来,不用CSS,不用图片,代码又少,呵呵