VB2008.NET DataGrid相关总结2

前端之家收集整理的这篇文章主要介绍了VB2008.NET DataGrid相关总结2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.VB2008.NET中DataGrid控件的使用

http://www.002pc.com/master/College/Programming/aspnet/13233.html

VB6 在,ADO是大家在数据中用得多的。而在新的vb.net中,然也可以用它来查询数据,但在与新的datagrid定中,不能像以前那使用:“datagrid1.datasoure=rs”,而是需要一定的化。因新的datagrid.datasoure对应的是 datatabledataset,而并不是原来的Recordset。所以,在使用中,要把Recordset转换datatable dataset

结合上一篇代码

 Function RecordsetToDataTable(ByVal adoRS As ADODB.Recordset,ByVal strTable As String)

 

        adoRS.MoveFirst()

        Dim dt As DataTable

        dt = New DataTable(strTable)

        Dim i As Integer

        Dim strcolname As String

        Dim t As Type

        Dim dr As DataRow

 

        For i = 0 To adoRS.Fields.Count - 1

            strcolname = adoRS.Fields(i).Name

            t = adoRS.Fields(i).Value.GetType()

            dt.Columns.Add(strcolname,t)

        Next

 

 

        While (Not adoRS.EOF)

            dr = dt.NewRow()

            For i = 0 To adoRS.Fields.Count - 1

                dr(i) = adoRS.Fields(i).Value

            Next

            dt.Rows.Add(dr)

            adoRS.MoveNext()

        End While

 

        Return dt

 

    End Function

 

 
    Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
        Con.ConnectionString = "Dbq=" & Application.StartupPath & "/Test.mdb;Driver={Microsoft Access Driver (*.mdb)}"
        Con.Open()
        Rec.CursorType = 1
        Rec.LockType = 3
        Rec.ActiveConnection = Con
        Rec.Open("Student") '("Select * From Student",Con)
        AxDataGrid1.DataSource = Rec
        AxDataGrid1.Refresh()
        Dim dts As DataTable
        'dts = RecordsetToDataTable(rsuser,"login") '其中rsuser为ado.Recordset,"login"为表名 
        dts = RecordsetToDataTable(Rec,"Student") 
        DataGrid1.DataSource = dts.DefaultView
    End Sub

2.运行程序后

猜你在找的VB相关文章