2009-04-27 23 views

答えて

4

これを行う最も簡単な方法は、長さが1の配列をバインドすることです。その中に何かを置くことができます。これは、これがダミー行であることを識別するのが好きです。 GridViewsのRowDataBoundメソッドで、データ項目がダミー行かどうかを確認します(データをチェックする前にRowTypeがDataRowであることを確認してください)。ダミー行であれば、行の可視性をfalseに設定します。フッターとヘッダーはデータなしで表示されるはずです。

GridViewでShowFooterプロパティをtrueに設定していることを確認してください。

例えば、

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostback) 
    { 
     myGrid.DataSource = new object[] {null}; 
     myGrid.DataBind(); 
    } 
}  

protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     if (e.Row.DataItem == null) 
     { 
      e.Row.Visible = false; 
     } 
    } 
} 
+0

これは、自動生成列== false、他のアイデアでは機能しませんか? – msbyuva

+0

これまで何度もやったことがあるはずです。ページの読み込み時に何かに縛られていることを確認しましたか? – Mike737

+0

グリッド定義でDataKeyNamesが指定されているため、これは私のためには機能しませんでした。 DataKeyNamesとグリッドが削除されました。データが表示されていないときにフッターだけが表示されている場合は通常通り表示されます。 – YeeHaw1234

0

Here is the simple way GridViewに空のデータがある場合にフッターを表示する。

/// <summary> 
    /// Ensures that the grid view will contain a footer even if no data exists. 
    /// </summary> 
    /// <typeparam name="T">Where t is equal to the type of data in the gridview.</typeparam> 
    /// <param name="gridView">The grid view who's footer must persist.</param> 
    public static void EnsureGridViewFooter<T>(GridView gridView) where T: new() 
    { 
     if (gridView == null) 
      throw new ArgumentNullException("gridView"); 

     if (gridView.DataSource != null && gridView.DataSource is IEnumerable<T> && (gridView.DataSource as IEnumerable<T>).Count() > 0) 
      return; 

     // If nothing has been assigned to the grid or it generated no rows we are going to add an empty one. 
     var emptySource = new List<T>(); 
     var blankItem = new T(); 
     emptySource.Add(blankItem); 
     gridView.DataSource = emptySource; 

     // On databinding make sure the empty row is set to invisible so it hides it from display. 
     gridView.RowDataBound += delegate(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.DataItem == (object)blankItem) 
       e.Row.Visible = false; 
     }; 
    } 

次を使用することができ、それを呼び出すために:ここで

+1

それは間違いなく良い考えです。 – abatishchev

2

は私が細工したことは簡単なものだ

 MyGridView.DataSource = data; 
     EnsureGridViewFooter<MyDataType>(MyGridView); 
     MyGridView.DataBind(); 

は、この情報がお役に立てば幸いです。乾杯!

関連する問題