vb.net 利用datagridview显示DB数据(复杂数据的取得和显示)

在文章http://blog.csdn.net/bigheadsheep/article/details/7711349 中已经介绍了datagridview直接显示数据库表的方法.

但是在实际操作中,更多的情况是需要先对显示的数据库表内容进行编辑,甚至是多个表结合之后的结果需要显示.

这时候能够将表内容的编辑结构,放到DataTable中的DataRow中,

先设定DataRow的列项目,然后在一条条的将输入导入DataRow,最后将DataRow追加到DataTable中

这样就可以实现复杂内容在datagridview中显示了

 


1 窗口form中追加控件datagridview
 
2 窗口form程序中设定:


   '整理数据并显示在gridview中
    Private Function editDataAndShow()
        '循环用变量
        Dim i As Integer
        Dim j As Integer

        '门店信息List
        Dim storeList As New ArrayList

        '人员信息List
        Dim dummymanList As New ArrayList

        '返回错误flag值
        Dim hasErrorFlg As Boolean = False

        '临时用Hashtable
        Dim tmpTable As New Hashtable

        'view显示用table
        Dim viewTable As New Hashtable

        'DataTable
        Dim table As New DataTable
        'datatable中使用的datarow
        Dim tableRow As DataRow
        Try
            '取得门店信息
            storeList = readDataFromDB.selectSellinStore()

            'gridview中需要显示的列,都要在DataTable中先定义
            '此处定义的顺序, 即gridview显示的顺序
            '门店信息
            table.Columns.Add("门店key")
            table.Columns.Add("门店类型")
            table.Columns.Add("门店ID")
            table.Columns.Add("门店名称(地址)")
            table.Columns.Add("地理城市")
            table.Columns.Add("地理大区")
            table.Columns.Add("销售归属城市")
            table.Columns.Add("销售归属大区")
            table.Columns.Add("销售归属公司")
            table.Columns.Add("渠道")
            table.Columns.Add("大客户名称")
            table.Columns.Add("客户详细名称")
            '门店状态
            table.Columns.Add("门店状态")

            '人员信息
            table.Columns.Add("SR1")
            table.Columns.Add("SR1销售所占百分比")
            table.Columns.Add("SR2")
            table.Columns.Add("SR2销售所占百分比")
            table.Columns.Add("SR3")
            table.Columns.Add("SR3销售所占百分比")
            table.Columns.Add("SR4")
            table.Columns.Add("SR4销售所占百分比")
            table.Columns.Add("SR5")
            table.Columns.Add("SR5销售所占百分比")
            table.Columns.Add("SR6")
            table.Columns.Add("SR6销售所占百分比")
            table.Columns.Add("SR7")
            table.Columns.Add("SR7销售所占百分比")
            table.Columns.Add("SR8")
            table.Columns.Add("SR8销售所占百分比")
            table.Columns.Add("SR9")
            table.Columns.Add("SR9销售所占百分比")
            table.Columns.Add("SR10")
            table.Columns.Add("SR10销售所占百分比")
            table.Columns.Add("SR11")
            table.Columns.Add("SR11销售所占百分比")
            table.Columns.Add("SR12")
            table.Columns.Add("SR12销售所占百分比")
            table.Columns.Add("SR13")
            table.Columns.Add("SR13销售所占百分比")
            table.Columns.Add("SR14")
            table.Columns.Add("SR14销售所占百分比")
            table.Columns.Add("SR15")
            table.Columns.Add("SR15销售所占百分比")
            table.Columns.Add("KAM")
            table.Columns.Add("DSM")
            'SR是否拆分
            table.Columns.Add("SR拆分区分")


            For i = 0 To storeList.Count - 1 Step 1


                '清空Hashtable
                tmpTable.Clear()
                viewTable.Clear()

                '将门店信息保存在table中,以便gridview显示
                tableRow = table.NewRow
                tableRow("门店key") = CType(storeList.Item(i), SellinStoreBean).storeKey.Trim
                tableRow("门店类型") = CType(storeList.Item(i), SellinStoreBean).storeType.Trim
                tableRow("门店ID") = CType(storeList.Item(i), SellinStoreBean).storeID.Trim
                tableRow("门店名称(地址)") = CType(storeList.Item(i), SellinStoreBean).storeName.Trim
                tableRow("地理城市") = CType(storeList.Item(i), SellinStoreBean).geoCity.Trim
                tableRow("地理大区") = CType(storeList.Item(i), SellinStoreBean).geoRegion.Trim
                tableRow("销售归属城市") = CType(storeList.Item(i), SellinStoreBean).salesCity.Trim
                tableRow("销售归属大区") = CType(storeList.Item(i), SellinStoreBean).salesRegion.Trim
                tableRow("销售归属公司") = CType(storeList.Item(i), SellinStoreBean).salesCompany.Trim
                tableRow("渠道") = CType(storeList.Item(i), SellinStoreBean).channel.Trim
                tableRow("大客户名称") = CType(storeList.Item(i), SellinStoreBean).cTotalName.Trim
                tableRow("客户详细名称") = CType(storeList.Item(i), SellinStoreBean).cDetailName.Trim
                tableRow("门店状态") = CType(storeList.Item(i), SellinStoreBean).status.Trim

                '取得DSM
                tmpTable.Add("storeKey", CType(storeList.Item(i), SellinStoreBean).storeKey.Trim)
                tmpTable.Add("positionLevel", Constant.DUMMYMAN_POSITION_DSM)
                tmpTable.Add("type", Constant.RELATION_TYPE_SELLIN)
                dummymanList = readDataFromDB.selectSellinRelationByLevel(tmpTable)
                '将DSM信息保存在table中,以便gridview显示(因为人员变动,DSM有可能为空,所以要追加判断)
                If dummymanList.Count <> 0 Then
                    tableRow("DSM") = CType(dummymanList.Item(0), Object)(0).ToString.Trim
                End If

                '清空Hashtable
                tmpTable.Clear()
                dummymanList.Clear()
                '取得KAM
                tmpTable.Add("storeKey", CType(storeList.Item(i), SellinStoreBean).storeKey.Trim)
                tmpTable.Add("positionLevel", Constant.DUMMYMAN_POSITION_KAM)
                tmpTable.Add("type", Constant.RELATION_TYPE_SELLIN)
                dummymanList = readDataFromDB.selectSellinRelationByLevel(tmpTable)


                '将KAM信息保存在table中,以便gridview显示(因为KAM有可能为空,所以要追加判断)
                If dummymanList.Count <> 0 Then
                    tableRow("KAM") = CType(dummymanList.Item(0), Object)(0).ToString.Trim
                End If

                '清空Hashtable
                tmpTable.Clear()
                dummymanList.Clear()
                '取得SR
                tmpTable.Add("storeKey", CType(storeList.Item(i), SellinStoreBean).storeKey.Trim)
                tmpTable.Add("positionLevel", Constant.DUMMYMAN_POSITION_SR)
                tmpTable.Add("type", Constant.RELATION_TYPE_SELLIN)
                dummymanList = readDataFromDB.selectSellinRelationByLevel(tmpTable)
                '将SR信息保存在table中,以便gridview显示
                For j = 0 To dummymanList.Count - 1 Step 1
                    tableRow("SR" & j + 1) = CType(dummymanList.Item(j), Object)(0).ToString.Trim
                    tableRow("SR" & j + 1 & "销售所占百分比") = CType(dummymanList.Item(j), Object)(1).ToString.Trim
                Next j

                If dummymanList.Count > 1 Then
                    tableRow("SR拆分区分") = Constant.SR_SPLIT_YES
                Else
                    tableRow("SR拆分区分") = Constant.SR_SPLIT_NO
                End If

 

                '将数据行保存到table中
                table.Rows.Add(tableRow)


            Next i

            '设定datagridview的数据源
            Me.DataGridView1.DataSource = table

            '隐藏行头
            Me.DataGridView1.RowHeadersVisible = False

            '返回错误flag值
            Return hasErrorFlg

        Catch ex As Exception
            MsgBox("更新数据库处理 editDataForUpdate()出错: " & ex.Message)
            Return True
        End Try

 

    End Function

你可能感兴趣的:(数据库,exception,object,table,Integer,VB.NET)