我正在使用Visual Studio 2012,我正在使用带有排序功能的GridView,排序工作完美,但我无法在标题部分显示图像(排序图像升序或降序)。我的代码是:
<asp:GridView ID="gv" runat="server" Width="50%" CssClass= "GridStyle-DarkGray" AllowSorting="True" AutoGenerateColumns="False" OnRowCreated="gv_RowCreated" OnSorting="gv_Sorting">
<Columns>
<asp:BoundField DataField="CompanyID" HeaderText="ID" SortExpression="CompanyID" />
<asp:BoundField DataField="CompanyName" HeaderText="Company Name" SortExpression="CompanyName" />
<asp:BoundField DataField="PhoneNo" HeaderText="Phone No" SortExpression="PhoneNo" />
</Columns>
</asp:GridView>
代码背后:
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
活动:
protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private void SortGridView(string sortExpression, string direction)
{
string conn = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=IMS;Integrated Security=True;";
string qry = "Select * from Company";
DataTable dt = new DataTable();
SqlDataAdapter DA = new SqlDataAdapter(qry, conn);
DA.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
if (dt != null)
{
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
gv.DataSource = dv;
gv.DataBind();
}
}
protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//Call the GetSortColumnIndex helper method to determine
//the index of the column being sorted.
int sortColumnIndex = GetSortColumnIndex();
if (sortColumnIndex != -1)
{
// Call the AddSortImage helper method to add
// a sort direction image to the appropriate
// column header.
AddSortImage(sortColumnIndex, e.Row);
}
}
}
int GetSortColumnIndex()
{
// Iterate through the Columns collection to determine the index
// of the column being sorted.
foreach (DataControlField field in gv.Columns)
{
if (field.SortExpression == gv.SortExpression)
{
return gv.Columns.IndexOf(field);
}
}
return -1;
}
// This is a helper method used to add a sort direction
// image to the header of the column being sorted.
void AddSortImage(int columnIndex, GridViewRow headerRow)
{
// Create the sorting image based on the sort direction.
Image sortImage = new Image();
if (gv.SortDirection == SortDirection.Ascending)
{
sortImage.ImageUrl = "~/Img/asc.gif";
Image1.ImageUrl = "~/img/asc.gif";
sortImage.AlternateText = "Ascending Order";
lab.Text = "Ascending";
}
else
{
sortImage.ImageUrl = "~/img/desc.gif";
Image1.ImageUrl = "~/img/desc.gif";
lab.Text = "Descending";
sortImage.AlternateText = "Descending Order";
}
// Add the image to the appropriate header cell.
headerRow.Cells[columnIndex].Controls.Add(sortImage);
}
我的代码有什么问题,它没有在标题中显示图片......?
答案 0 :(得分:0)
在Code Behind中添加此内容
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Image img = new Image();
img.ImageUrl = "~/Contents/Images/asc.png";
GridView1.HeaderRow.Cells[1].Controls.Add(new LiteralControl(" "));
GridView1.HeaderRow.Cells[1].Controls.Add(img);
}
}
使用图像单元格索引更改单元格索引