在一个页面上建一个datagrid表不做任何数据库连接,但又可以浏览出表头来?

解决方案 »

  1.   

    定一个newtable,然后加一些列,绑定就可以了!
      

  2.   

    是的用空表做数据源。怎样定一个newtable呢?
      

  3.   

    用程序生成表头吧:
    在DataGrid的ItemCreated事件里面定义:
    代码如下:if ( e.Item.ItemType == ListItemType.Header ) 
    {
    TableCellCollection tcl = e.Item.Cells;//获得表头元素的实例
    tcl.Clear();//清除原有控件表头 DataGridItem dgi = new DataGridItem(0,-1,ListItemType.Header);
    DataGridItem dgi1= new DataGridItem(0,-1,ListItemType.Header);
    Table tb = new Table();
    tb = (Table)DataGrid1.Controls[0];
    tb.Rows.AddAt(0,dgi);
    tb.Rows.AddAt(1,dgi1);
    TableCell tc11 = new TableCell();
    TableCell tc12 = new TableCell();
    TableCell tc13 = new TableCell();
    TableCell tc21 = new TableCell();
    TableCell tc22 = new TableCell();
    TableCell tc23 = new TableCell();
    TableCell tc24 = new TableCell();
    TableCell tc25 = new TableCell();
    tc11.Text = "11";
    tc11.ColumnSpan = 1;
    tcl1.RowSpan = 2;
    dgi.Cells.Add(tc11); tc12.Text = "12";
    tc12.ColumnSpan = 3;
    dgi.Cells.Add(tc12); tc13.Text = "13";
    tc13.ColumnSpan = 2;
    dgi.Cells.Add(tc13); tc21.Text = "21";
    tc21.ColumnSpan = 1;
    dgi1.Cells.Add(tc21);  tc22.Text = "22";
    tc22.ColumnSpan = 1;
    dgi1.Cells.Add(tc22);
    //
    tc23.Text = "23";
    tc23.ColumnSpan = 1;
    dgi1.Cells.Add(tc23);
                        //
    tc24.Text = "24";
    tc24.ColumnSpan = 1;
    dgi1.Cells.Add(tc24); tc25.Text = "25";
    tc25.ColumnSpan = 1;
    dgi1.Cells.Add(tc25);


    }
      

  4.   

    绑定的时候,sql语句加上Where条件,让该查询一条记录也查不出来,再绑定不就可以了么,这样是做了数据连接的,不过别没什么好的办法!我都是这样用的!
      

  5.   

    如何建一空表?DataTable dt = new DataTable("test")
    dt.Columns.Add(...)
    .....
    ....