DataTable绑定Table控件中的数据(vb.net之经典应用)

  

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

Sub bind_table(ByVal tbl As DataTable)
Dim cn As New SqlClient.SqlConnection(P_Con)
        
Dim cmd As SqlClient.SqlCommand = cn.CreateCommand
        
Dim adpt As New SqlClient.SqlDataAdapter(cmd)
        
Dim tbl As New DataTable                                  // 数据绑定,绑定Table表          
        cmd.CommandTimeout 
= 3000
        
Try
         cmd.CommandText 
= str
    adpt.Fill(tbl)
        
Dim i As Integer
        
Dim j As Integer
        
Dim trblm As DataRow = 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对象中(指定位置的插入操作)
 
        
For i = 0 To tbl.Rows.Count – 1    //统计当前dataTable表中所有行的个数
            
Dim tr As New UI.WebControls.TableRow   //(此处定义的是一个TableRow控件)
            
For j = 0 To tbl.Columns.Count – 1    //统计当前dataTable表中列的个数
                
Dim thecell As New UI.WebControls.TableCell//此处定义了一个单元格
                thecell.Font.Size 
= FontUnit.Point(9)
                thecell.BorderStyle 
= BorderStyle.Solid //给每一个单元格的属性赋值
                thecell.BorderWidth 
= Unit.Point(1)   //指定了当前表的边框宽度,控制表的美观
                
If IsNumeric(IIf(tbl.Rows(i)(j) Is DBNull.Value, "", tbl.Rows(i)(j))) = True Then
                    thecell.HorizontalAlign 
= HorizontalAlign.Right(指定对齐方式为右对齐)
//指定表中内容的对齐方式,用IF语句判断是数字类型还是文字类型,如为数字类型,则左对齐,反之,右对齐
                
Else
                    thecell.HorizontalAlign 
= HorizontalAlign.Left(指定对齐方式为左对齐)
                
End If
                thecell.Wrap 
= False
               thecell.Height 
= Unit.Pixel(20)//指定当前单元格的高度和宽度
                thecell.Width 
= Unit.Pixel(80)
                thecell.RowSpan 
= 1
                
If i = 0 And (j = 4 or j = 7 or j = 10Then
                    thecell.Text 
= ""
                
Else
//本循环体中最重要的一句代码
                    thecell.Text 
= IIf(tbl.Rows(i)(j) Is DBNull.Value, "", tbl.Rows(i)(j))
                
End If
                tr.Cells.Add(thecell)  
//往TableRow控件添加单元格操作,
//循环一次添加一行。直到DataTable中数据全部读出为止,这样就实现了将数据虚拟表(DataTable)中的数据全部绑定在Table控件上
            
Next
            Table2.Rows.Add(tr)   
//往Table控件中添加行操作
        
Next
    
End Sub

//***************************************下方是一个比较全的例子****************************************
  

 

   Dim  cn  As   New  SqlClient.SqlConnection(P_Con)

        
Dim  cmd  As  SqlClient.SqlCommand  =  cn.CreateCommand

        
Dim  adpt  As   New  SqlClient.SqlDataAdapter(cmd)

        
Dim  tbl  As   New  DataTable

        cmd.CommandTimeout 
=   3000

        
Try

            cmd.CommandText 
=   str

            adpt.Fill (tbl)

 

            
Dim  tr  As  DataRow  =  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)

        
Catch  ex  As  Exception

            
Throw  ex

        
End   Try

   
End Sub

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

    
Sub bind_table(ByVal tbl As DataTable)

        
Dim i As Integer

        
Dim j As Integer

        
For i = 0 To tbl.Rows.Count - 1

            
Dim tr As New UI.WebControls.TableRow

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

                
Dim thecell As New UI.WebControls.TableCell

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

thecell.BorderStyle 
= BorderStyle.Solid

thecell.BorderWidth 
= Unit.Point(1)

If IsNumeric(IIf(tbl.Rows(i)(j) Is DBNull.Value, "", tbl.Rows(i)(j))) = True Then

    thecell.HorizontalAlign 
= HorizontalAlign.Right

Else

    thecell.HorizontalAlign 
= HorizontalAlign.Left

End If

thecell.Wrap 
= False

thecell.Height 
= Unit.Pixel(20)

thecell.Width 
= Unit.Pixel (100)

thecell.RowSpan 
= 1

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

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

            
Next

 

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

        
Next

 

End Sub


 

下方的图是运行效果

 

你可能感兴趣的:(asp.net)