2014-02-08 106 views
0

我使用的是Visual Studio 2012,我正在使用GridView的排序功能,完美的排序工作,但我无法显示图像(排序图像升序或降序)在标题部分。我的代码是:图像不会显示在ASP.Net中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); 
} 

这有什么错我的代码,它并没有在标题显示图像.. 。?

+4

使用一个CSS类中提供的图片作为背景添加这并应用CSS类到你的''标签 –

回答

0

在代码隐藏

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); 
    } 
} 

更改单元格指数与图像细胞指数

+0

它没有为我工作, –

+0

什么错误让你用这个 –

+0

我没有得到任何错误,但是当我运行我的程序时,它仍然没有在头部显示任何图像。图像路径也是正确的。 –