如我有SQL查询出来的一个datatable
ID   Name  Price
1    小王   1000
2    小张   1200
3    小李   1300现在要求将他们绑定在我的一个HTML控件的Table上
注意 是一个Table

效果如
ID    1     2    3
Name  小王   小张  小李
Price 1000  1200 1300谢谢 急

解决方案 »

  1.   

    循环绑呗
    动态生成一个tableTable t1 = new Table();
    TableCell tcSystem = new TableCell();
    tcSystem.Controls.Add(cbSystem);
    TableRow trSystem = new TableRow();
    trSystem.Controls.Add(tcSystem);
    t1.Controls.Add(trSystem);
      

  2.   

    没有直接绑定,只能用循环把Table显示出来。
      

  3.   

    <%@ Page Language="C#" %><%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Drawing" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    DataTable dt = new DataTable();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("Price");
            if (!IsPostBack)
            {
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                dt.Rows[0][0] = "1";
                dt.Rows[0][1] = "小张";
                dt.Rows[0][2] = "1000";
                dt.Rows[1][0] = "2";
                dt.Rows[1][1] = "小王";
                dt.Rows[1][2] = "1200";
                dt.Rows[2][0] = "2";
                dt.Rows[2][1] = "小李";
                dt.Rows[2][2] = "1300";
            }
            else
            {
                
            }
            TableRow tr1 = new TableRow();
            TableRow tr2 = new TableRow();
            TableRow tr3 = new TableRow();
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            TableCell tc3 = new TableCell();
            tc1.Text = "ID";
            tr1.Cells.Add(tc1);
            tc2.Text = "Name";
            tr2.Cells.Add(tc2);
            tc3.Text = "Price";
            tr3.Cells.Add(tc3);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TableCell tcID = new TableCell();
                tcID.Text = (string)dt.Rows[i][0];
                tr1.Cells.Add(tcID);
                TableCell tcName = new TableCell();
                tcName.Text = (string)dt.Rows[i][1];
                tr2.Cells.Add(tcName);
                TableCell tcPrice = new TableCell();
                tcPrice.Text = (string)dt.Rows[i][2];
                tr3.Cells.Add(tcPrice);
            }
            this.Table1.Rows.Add(tr1);
            this.Table1.Rows.Add(tr2);
            this.Table1.Rows.Add(tr3);
        }</script><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <asp:Table ID="Table1" runat="server" BorderStyle="Solid"></asp:Table> 
            </div>
        </form>
    </body>
    </html>
      

  4.   


    private void TableBind()
        {
            DataTable dt = DataTable();
            TableRow tblr = new TableRow();
            tblr.BackColor = System.Drawing.Color.Tomato;        foreach (DataColumn dc in dt.Columns)
            {
                TableCell tblc = new TableCell();
                tblc.Text = dc.ColumnName.ToString();
                tblr.Cells.Add(tblc);
            }
            this.tab1.Rows.Add(tblr);        foreach (DataRow dr in dt.Rows)
            {
                TableRow tr2 = new TableRow();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    TableCell tc = new TableCell();
                    tc.Text = dr[i].ToString();
                    tr2.Cells.Add(tc);
                }
                this.tab1.Rows.Add(tr2);
            }
        }
      

  5.   

    楼主马甲超级多啊如果用<div>css显示的话,更容易些
      

  6.   

            public DataSet getds(string sql)
            {            thisConnection.Open();            SqlDataAdapter thisAdapater = new SqlDataAdapter(sql, thisConnection);            DataSet thisDataSet = new DataSet();            thisAdapater.Fill(thisDataSet);            thisConnection.Close();            return thisDataSet;
            }
      

  7.   

    为什么要自己写代码?用repeater很方便啊。
      

  8.   

    建议使用Repeater,请看参考以下代码:
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            string connectionString = GetConnectionString();
            Repeater.DataSource = GetDataSet(connectionString);
            Repeater.DataBind();
        }    private DataSet GetDataSet(string connectionString)
        {
            DataSet dataSet;        using (SqlConnection connection =
                       new SqlConnection(connectionString))
            {
                SqlDataAdapter adapter = new SqlDataAdapter();            adapter.TableMappings.Add("Table", "Suppliers");            connection.Open();            SqlCommand command = new SqlCommand(
                    @"SELECT TOP 20 Country, CompanyName, ContactName
                    FROM dbo.Suppliers ORDER BY Country;",
                    connection);
                command.CommandType = CommandType.Text;            adapter.SelectCommand = command;            dataSet = new DataSet("Suppliers");
                adapter.Fill(dataSet);            connection.Close();
            }        return dataSet;
        }    static private string GetConnectionString()
        {
            return "Data Source=(local);Initial Catalog=Northwind;"
                + "Integrated Security=SSPI";
        }
    </script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater" runat="server">
                <HeaderTemplate>
                <table border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
                    <tr>
                        <td><strong>Country</strong></td>
                        <td><strong>CompanyName</strong></td>
                        <td><strong>ContactName</strong></td>
                    </tr>
                </HeaderTemplate>
                <ItemTemplate>
                        <tr>
                            <td>'<%# Eval("Country") %></td>
                            <td>'<%# Eval("CompanyName") %></td>
                            <td>'<%# Eval("ContactName") %></td>
                        </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </div>
        </form>
    </body>
    </html>