如果Gridview 的DataSouce is null,绑定时是一片白,
目前要做到如果数据源为空时.1 要显示列头,
2 Gridview 显示5行空表格.
请大侠指点.网上找的代码,只能完成1,我还不知道怎么用,菜了. 
  /// 
  /// GridView 扩展控件 
  /// @author:[email protected] 
  /// 
  public class GridView : System.Web.UI.WebControls.GridView 
  { 
  private bool _enableEmptyContentRender = true ; 
  /// 
  /// 是否数据为空时显示标题行 
  /// 
  public bool EnableEmptyContentRender 
  { 
  set { _enableEmptyContentRender = value; } 
  get { return _enableEmptyContentRender; } 
  } 
  private string _EmptyDataCellCssClass ; 
  /// 
  /// 为空时信息单元格样式类 
  /// 
  public string EmptyDataCellCssClass 
  { 
  set { _EmptyDataCellCssClass = value ; } 
  get { return _EmptyDataCellCssClass ; } 
  } 
  /// 
  /// 为空时输出内容 
  /// 
  /// 
  protected virtual void RenderEmptyContent(HtmlTextWriter writer) 
  { 
  Table t = new Table(); //create a table 
  t.CssClass = this.CssClass; //copy all property 
  t.GridLines = this.GridLines; 
  t.BorderStyle = this.BorderStyle; 
  t.BorderWidth = this.BorderWidth; 
  t.CellPadding = this.CellPadding; 
  t.CellSpacing = this.CellSpacing; 
  t.HorizontalAlign = this.HorizontalAlign; 
  t.Width = this.Width; 
  t.CopyBaseAttributes(this); 
  TableRow row = new TableRow(); 
  t.Rows.Add(row); 
  foreach (DataControlField f in this.Columns) //generate table header 
  { 
  TableCell cell = new TableCell(); 
  cell.Text = f.HeaderText; 
  cell.CssClass = "TdHeaderStyle1"; //这里把表头样式写死了 
  row.Cells.Add(cell); 
  } 
  TableRow row2 = new TableRow(); 
  t.Rows.Add(row2); 
  TableCell msgCell = new TableCell(); 
  msgCell.CssClass = this._EmptyDataCellCssClass; 
  if (this.EmptyDataTemplate != null) //the second row, use the template 
  { 
  this.EmptyDataTemplate.InstantiateIn(msgCell); 
  } 
  else //the second row, use the EmptyDataText 

解决方案 »

  1.   

    点GridView/编辑模板/EmptyDataTemplate
    中加入你想要的东东!!
      

  2.   


    DataTable dTable = new DataTable ();
    dTable.Columns .Add ("列名1",typeof(string));
    dTable.Columns .Add ("列名2",typeof(string));
    dTable.Columns .Add ("列名3",typeof(string));
    for(int i=0;i<5;i++)
    {
    DataRow dr = dTable.NewRow ();
    dr["列名1"] = "" ;
    dr["列名2"] = "" ;
    dr["列名3"] = "" ;
    dTable.Rows .Add (dr);
    }
    this.DataGrid1.DataSource =dTable;
    this.DataGrid1.DataBind();
    就可以保存样式和标题~`~~
      

  3.   

    你绑定的数据~~DataTable、或DataSet.Tables[0]为空的时候~你就绑定DataTable dTable = new DataTable ();中的dTable列名你自己加,多少行你在for循环判断!
      

  4.   

    我忘说了一个问题,就是在Gridview不足5行的情况下.要显示5行,没数据的只显示单元格
      

  5.   

    像liaoyunt07那样的,如果数据源不为空,绑定数据源,为空的话,新建一个空的(5行)数据源绑定到控件。
      

  6.   

    要不然自己手动做个Table,根据datasourse控制其显示也可以啊
      

  7.   

    gridview 有个可以设定无数据时显示的simpale
    <EmptyDataTemplate>
    //这里写你在无数据时要显示的样式
    如果只要表头
    <table>
    内容
    </table>
            </EmptyDataTemplate>