前台页面代码 Default.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebAppTest._Default" %> <!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>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Table ID="Table1" runat="server" Height="190px" Width="253px"> </asp:Table> </div> </form> </body> </html> 后台页面代码Default.aspx.cs: using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; namespace WebAppTest { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string connString = "Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=123456"; SqlConnection conn = new SqlConnection(connString); SqlDataAdapter sda = new SqlDataAdapter("select * from Categories", conn); DataSet dataset = new DataSet(); sda.Fill(dataset); DataTable dt = dataset.Tables[0]; double d = dt.Rows.Count / 2; int rowCount=int.Parse (Math.Ceiling (d ).ToString ()); for(int i=0,k=0;i<rowCount;i++,k=k+2) { TableRow tRow = new TableRow(); Table1.Rows.Add(tRow); for (int j = 0; j < 2; j++) { TableCell tCell = new TableCell(); tCell.Text = string.Format("{0}、{1}", (k+ j).ToString(), dt.Rows[k+j][1].ToString()); tRow.Cells.Add(tCell); } } } } }