2014-02-08 102 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

我沒有得到任何錯誤,但是當我運行我的程序時,它仍然沒有在頭部顯示任何圖像。圖像路徑也是正確的。 –