2011-07-25 6 views
1

以下のコードは、GridViewのソート時にGridView SortExpressionがNULLを返すときの昇順降順イメージの追加を示しています。Gridview SortExpressionが空です

これはなぜ起こっているのですか?

protected void grvSample_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      foreach (TableCell tc in e.Row.Cells) 
      { 
       if (tc.HasControls()) 
       { 
        LinkButton lnk = (LinkButton)tc.Controls[0]; 
        if (lnk != null) 
        { 
         System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); 
         img.ImageUrl = "~/Images/" + (grvSample.SortDirection == SortDirection.Ascending ? "asc" : "desc") + ".gif"; 
         if (grvSample.SortExpression == lnk.CommandArgument) 
         { 
          // adding a space and the image to the header link 
          tc.Controls.Add(new LiteralControl(" ")); 
          tc.Controls.Add(img); 
         } 
        } 
       } 
      } 
     } 
    } 

答えて

2

行が作成されたときに設定されるとは思われません。並べ替えの列がクリックされると設定されます。

関連する問題