//把tbl传递过来时一定要指定其类型,不然会出现对象未实例化

Subbind_table()Subbind_table(ByValtblAsDataTable)
DimcnAsNewSqlClient.SqlConnection(P_Con)
DimcmdAsSqlClient.SqlCommand=cn.CreateCommand
DimadptAsNewSqlClient.SqlDataAdapter(cmd)
DimtblAsNewDataTable//数据绑定,绑定Table表
cmd.CommandTimeout=3000
Try
cmd.CommandText=str
adpt.Fill(tbl)
DimiAsInteger
DimjAsInteger
DimtrblmAsDataRow=tbl.NewRow先新增一行,并给该行的元素赋初始值
trblm.Item(0)="合计"
trblm.Item(1)=tbl.Compute("sum(inum)","")
trblm.Item("heji")=tbl.Compute("sum(heji)","")//对heji字段这一列,进行求和运算运算
trblm.Item("zdinum")=tbl.Compute("sum(zdinum)","")
trblm.Item("zdiprice")=tbl.Compute("sum(zdiprice)","")
trblm.Item("zdheji")=tbl.Compute("sum(zdheji)","")
trblm.Item("ewinum")=tbl.Compute("sum(ewinum)","")
trblm.Item("ewiprice")=tbl.Compute("sum(ewiprice)","")
trblm.Item("ewheji")=tbl.Compute("sum(ewheji)","")
trblm.Item("xpinum")=tbl.Compute("sum(xpinum)","")
trblm.Item("xpiprice")=tbl.Compute("sum(xpiprice)","")
trblm.Item("xpheji")=tbl.Compute("sum(xpheji)","")
tbl.Rows.InsertAt(trblm,0)//将合计插入第一行,插入到DataTable对象中(指定位置的插入操作)

Fori=0Totbl.Rows.Count–1 //统计当前dataTable表中所有行的个数
DimtrAsNewUI.WebControls.TableRow //(此处定义的是一个TableRow控件)
Forj=0Totbl.Columns.Count–1 //统计当前dataTable表中列的个数
DimthecellAsNewUI.WebControls.TableCell//此处定义了一个单元格
thecell.Font.Size=FontUnit.Point(9)
thecell.BorderStyle=BorderStyle.Solid//给每一个单元格的属性赋值
thecell.BorderWidth=Unit.Point(1) //指定了当前表的边框宽度,控制表的美观
IfIsNumeric(IIf(tbl.Rows(i)(j)IsDBNull.Value,"",tbl.Rows(i)(j)))=TrueThen
thecell.HorizontalAlign=HorizontalAlign.Right(指定对齐方式为右对齐)
//指定表中内容的对齐方式,用IF语句判断是数字类型还是文字类型,如为数字类型,则左对齐,反之,右对齐
Else
thecell.HorizontalAlign=HorizontalAlign.Left(指定对齐方式为左对齐)
EndIf
thecell.Wrap=False
thecell.Height=Unit.Pixel(20)//指定当前单元格的高度和宽度
thecell.Width=Unit.Pixel(80)
thecell.RowSpan=1
Ifi=0And(j=4orj=7orj=10)Then
thecell.Text=""
Else
//本循环体中最重要的一句代码
thecell.Text=IIf(tbl.Rows(i)(j)IsDBNull.Value,"",tbl.Rows(i)(j))
EndIf
tr.Cells.Add(thecell) //往TableRow控件添加单元格操作,
//循环一次添加一行。直到DataTable中数据全部读出为止,这样就实现了将数据虚拟表(DataTable)中的数据全部绑定在Table控件上
Next
Table2.Rows.Add(tr) //往Table控件中添加行操作
Next
EndSub
//***************************************下方是一个比较全的例子****************************************
DimcnAsNewSqlClient.SqlConnection(P_Con)

DimcmdAsSqlClient.SqlCommand=cn.CreateCommand

DimadptAsNewSqlClient.SqlDataAdapter(cmd)

DimtblAsNewDataTable

cmd.CommandTimeout=3000

Try

cmd.CommandText=str

adpt.Fill(tbl)



DimtrAsDataRow=tbl.NewRow'生成一个空的数据行(DataTable类型)



tr.Item("cDepCode")=""

tr.Item("cDepName")=""

tr.Item("客户经理")="合计"

tr.Item("累计预算营销费用")=tbl.Compute("sum(累计预算营销费用)","")

tr.Item("累计实际营销费用")=tbl.Compute("sum(累计实际营销费用)","")

tr.Item("余额")=tbl.Compute("sum(余额)","")

tr.Item("实际销额")=tbl.Compute("sum(实际销额)","")

tr.Item("实际销量")=tbl.Compute("sum(实际销量)","")

tr.Item("实际提取营销费")=tbl.Compute("sum(实际提取营销费)","")

tr.Item("实际广告费用")=tbl.Compute("sum(实际广告费用)","")

tr.Item("实际营销费用")=tbl.Compute("sum(实际营销费用)","")

tr.Item("累计营销费用余额")=tbl.Compute("sum(累计营销费用余额)","")

tr.Item("定单销额")=tbl.Compute("sum(定单销额)","")

tr.Item("定单销量")=tbl.Compute("sum(定单销量)","")

tr.Item("订单提取营销费")=tbl.Compute("sum(订单提取营销费)","")

tr.Item("合计")=tbl.Compute("sum(合计)","")

tr.Item("扣除项目")=tbl.Compute("sum(扣除项目)","")

tr.Item("系统已有余额")=tbl.Compute("sum(系统已有余额)","")

tr.Item("本月追加进系统")=tbl.Compute("sum(本月追加进系统)","")

tr.Item("小计")=tbl.Compute("sum(小计)","")

tr.Item("列入营销费预算")=tbl.Compute("sum(列入营销费预算)","")



tbl.Rows.InsertAt(tr,0)'将合计插入到第一行,此处的0指的是数据插入的位置

bind_table(tbl)

CatchexAsException

Throwex

EndTry

EndSub

//===============================================


Subbind_table()Subbind_table(ByValtblAsDataTable)

DimiAsInteger

DimjAsInteger

Fori=0Totbl.Rows.Count-1

DimtrAsNewUI.WebControls.TableRow

Forj=2Totbl.Columns.Count–1//j从2读数据,是因为前两条记录设为空

DimthecellAsNewUI.WebControls.TableCell

thecell.Font.Size=FontUnit.Point(9)//字体的单位是fontunit开始

thecell.BorderStyle=BorderStyle.Solid

thecell.BorderWidth=Unit.Point(1)

IfIsNumeric(IIf(tbl.Rows(i)(j)IsDBNull.Value,"",tbl.Rows(i)(j)))=TrueThen

thecell.HorizontalAlign=HorizontalAlign.Right

Else

thecell.HorizontalAlign=HorizontalAlign.Left

EndIf

thecell.Wrap=False

thecell.Height=Unit.Pixel(20)

thecell.Width=Unit.Pixel(100)

thecell.RowSpan=1

thecell.Text=IIf(tbl.Rows(i)(j)IsDBNull.Value,"",tbl.Rows(i)(j))//给单元格赋值

tr.Cells.Add(thecell)//使用循环读出Table表中所有的数据并转传给Table控件的单元格来接收

Next



Table2.Rows.Add(tr)//Table2为服务器控件Table的ID

Next



EndSub

下方的图是运行效果