这个功能不是DataGrid做的,而是要看你的Sql语句如何写.

解决方案 »

  1.   

    DataGrid的数据源(DataSource)可以是DataSet,DataTable,DataView,数组等你将要显示的数据放到以上数据源对象中即可至于怎么放,这是Ado.net的东西
      

  2.   

    如果是分别显示不同的表的内容,那就简单,加一个datagrid控件. 使用一个数据库查询,返回某个表的数据,并绑定到该 datagrid 上就可以了。如果同时显示多个表的内容,就用一个关联查询(join)来连接多个表。下面是一段例子:<%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %><html>
    <script language="C#" runat="server">    protected void Page_Load(Object sender, EventArgs e) {
            SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
            SqlCommand myCommand = new SqlCommand("select * from Authors", myConnection);        myConnection.Open();        SqlDataReader dr = myCommand.ExecuteReader();        MyDataGrid.DataSource = dr;
            MyDataGrid.DataBind();        myConnection.Close();
        }</script><body>  <h3><font face="Verdana">Simple Select to a DataGrid Control</font></h3>  <ASP:DataGrid id="MyDataGrid" runat="server"
        Width="700"
        BackColor="#ccccff"
        BorderColor="black"
        ShowFooter="false"
        CellPadding=3
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
        EnableViewState="false"
      /></body>
    </html>
      

  3.   

    1. get the table list
    2. get data for each table
    3. bind the data to a datagridhere is a simple demo:<%@ Page Language="C#" %>
    <%@ Import Namespace="System.Collections" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <script runat="server">
    void Page_Load(Object sender, EventArgs e)
    {
      String sConn = "Server=localhost;Database=pubs;UID=sa;PWD=;";
      SqlConnection conn = new SqlConnection(sConn);  
      SqlCommand com = new SqlCommand("select name from sysobjects where type='U'", conn);
      conn.Open();
      SqlDataReader dr = com.ExecuteReader();
      ArrayList tableList = new ArrayList();
      while (dr.Read())
        tableList.Add(dr.GetString(0));
      dr.Close();
      foreach (String sName in tableList)
      {
    Literal li = new Literal();
    li.Text = sName + "<BR>";
    this.Controls.Add(li);
    com = new SqlCommand("select * from " + sName, conn);
    dr = com.ExecuteReader();
    DataGrid dg = new DataGrid();
    this.Controls.Add(dg);
    dg.DataSource = dr;
    dg.DataBind();
    dr.Close();
      }  conn.Close();
    }
    </script>
      

  4.   

    to spring_ok(spring.z) :
    jion在哪里用??怎样用?为什么你的例子中看不到?
      

  5.   

    我的例子是显示一个表的。想同时显示多个表,就将SQL语句修改:
    比如:
    SqlCommand myCommand = new SqlCommand("select * from Authors", myConnection);
    改成:
    SqlCommand myCommand = new SqlCommand("select Authors.*, Titles.* from Authors inner join Titles on Authors.au_id = Titles.au_id", myConnection);