2009-04-27 4 views

답변

4

가장 쉬운 방법은 길이가 1 인 배열을 바인딩하는 것입니다. 이 행에 무언가를 넣을 수 있습니다. GridViews RowDataBound 메서드에서 데이터 항목이 더미 행인지 확인합니다 (데이터를 확인하기 전에 먼저 RowType이 DataRow인지 확인하십시오). dummy 행의 경우 행 가시성을 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

2

여기에 내가 제작 한 것을 쉽게 뭔가 :이 도움이

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

희망 :

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

는 다음과 같은 사용할 수 있습니다를 호출하십시오. 건배!

관련 문제