.NET2005,怎么用代码创建表格,而且把数据写到创建的表格里。

解决方案 »

  1.   

    05里面不是有Table这个控件吗
    这样写就行了
    Radi rad = new Radi();
            rad.RepeatDirection = RepeatDirection.Horizontal;
            TextBox box = new TextBox();
            box.TextMode = TextBoxMode.Password;
            rad.Items.Add("男"); rad.Items.Add("女");
            cob com = new cob();
            com.Items.Add("本科");com.Items.Add("大专");com.Items.Add("硕士");
            this.Table1.Rows[0].Cells[1].Controls.Add(new TextBox());
            this.Table1.Rows[0].Cells[3].Controls.Add(box);
            this.Table1.Rows[1].Cells[1].Controls.Add(new TextBox());
            this.Table1.Rows[1].Cells[3].Controls.Add(rad);
            this.Table1.Rows[2].Cells[1].Controls.Add(com);
            this.Table1.Rows[2].Cells[3].Controls.Add(new TextBox());
      

  2.   


    <style type="text/css">
            .tabStyle { color: #333; background-color:Maroon;}
            .tabStyle td { margin: 1px; padding: 2px; background-color:White; }
            
    </style>protected void Page_Load(object sender, EventArgs e)
    {
            Table tb = new Table();
            using (SqlConnection conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["连接字符串"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select 字段名1, 字段名2 from 表名";            conn.Open();
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    TableRow row = new TableRow();                TableCell cell = new TableCell();                cell.Text = reader["字段名1"].ToString();
                    row.Cells.Add(cell);                cell = new TableCell();
                    cell.Text = reader["字段名2"].ToString();
                    row.Cells.Add(cell);                tb.Rows.Add(row);
                }
                
            }
            tb.CssClass = "tabStyle";
            this.Controls.Add(tb);}