DataGrid不会显示数据集/记录集

时间:2012-06-04 02:00:07

标签: database vb.net datagridview

我认为自己是一个非常有耐心的人,有很大的决心让事情发挥作用,比如我正在研究的一个项目。然而,我觉得我只是浪费宝贵的时间,我可以用来实际获取知识,而不是碰到我的头撞墙完成任何事情。所以..我打算假装我今天花了11个小时试图修复这个问题并没有浪费,只要能够解决它。 我是数据库的新手,正在开发数据库程序。它是用vb6编写的,我试图在vb.net中重写它。我已经完全从所有不需要的花哨插件中删除了程序,现在只需要运行它所需的代码。程序会检索记录,但不会将这些记录填充到数据网格中。是的,我确实试图在其他帖子和谷歌搜索上找到帮助,但我无法得到任何适用于我的解决方案。

Form1中

Imports ADS_SQL_TEST_VBNET
Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Module1.Main()
End Sub

Public Sub cmdSQLBound_Click()
sWhichDataType = "SQL"
Call SetGridSize("SQL Table Bound.")
End Sub

Public Sub optYes01ph_Click()
BuildTable()         'sets sFields and sQuery
cmdSQLBound_Click()
End Sub

End Class

模块1

Imports System.Data
Imports System.Data.SqlClient
Imports ADS_SQL_TEST_VBNET.Form1
Imports ADS_SQL_TEST_VBNET.Class1

Public Module Module1

Public rs As ADODB.Recordset
Public db As ADODB.Connection

Public dgvDataGrid As DataGridView
Public DataGridRef As New DataGridView
Public sWhichDataType, sSource, sTable, sFields, sQuery, sFilter, sOrderBy, sSelectedIndex, sStoreId    
Public lblCount As Object
Public Ctrl1, Ctrl2, Freq, Overhead As Decimal
Public l, x As Integer
Public A, I As Long


Public Sub Main()

  Form1.optYes01ph_Click()

End Sub


Public Sub BuildTable()
    If sTable = "yes01ph" Or sTable = "" Then
        sTable = "yes01ph"
        sFields = "store_id,control_no,first_name,last_name,company,address,city,State,zip,phone,total,stat_code,Year,make_id,model_id,engine_id,contact,phone1,phone2,phone3,phone4,invoice_no,ready_proc,summary,note"
        sOrderBy = "control_no"
    Else
        sFields = "store_id,ml_id,first_name,last_name,company,address,city,state,zip,phone,last_upd,address2,phone2,ref_count,comments,fobs_key,quest,lockedby,rmte_stat,serv_upd,serv_id,psm_use,psm_points"
        sOrderBy = "ml_id"
    End If
    BuildFilter()
End Sub

Public Sub BuildFilter()
    sFilter = " AND control_no > 1 AND control_no < 500000"
    BuildQuery()
End Sub

Public Sub BuildQuery()

    sQuery = "SELECT " & sFields & " FROM " & sTable & " WHERE Store_ID Like 'XXX' " & sFilter & " Order By " & sOrderBy
    sQuery = Replace(sQuery, "'XXX'", "'" & Trim(UCase("250")) & "'")
End Sub



Public Sub SetGridSize(ByVal sMessage As String)

    Dim lblTableType As String
    lblTableType = "  " & sMessage
    Form1.dgvDataGrid.Text = "Data populated using: " & UCase(sMessage)

    DataGridRef.ColumnCount = 99
    DataGridRef.RowCount = 1

    DataSetRef.LoadDataBound()
End Sub
End Module

的Class1

Public Class Class1

Public Sub LoadDataBound()

rs = GetData()
If Not rs.EOF Then
    dgvDataGrid.DataSource = rs '     Bind the Datagrid to the recordset
    dgvDataGrid.Refresh()
Else
    MsgBox("No Records Found in DataGrid!")
End If
End Sub

Public Function GetData() As ADODB.Recordset
sWhichDataType = UCase(Trim(sWhichDataType))

sSource = "development"
GetData = OpenSQL()
End Function

Public Function OpenSQL() As ADODB.Recordset
db = New ADODB.Connection : rs = New ADODB.Recordset ' Initialize Connection object &    RecordSet object

With db
    .CursorLocation = ADODB.CursorLocationEnum.adUseClient
    .Mode = ADODB.ConnectModeEnum.adModeReadWrite
    .ConnectionString = "Provider=SQLOLEDB.1;Password=pacesql;Persist Security Info=True;User ID=sa;Initial Catalog=speedwrench;Data Source=" & sSource
    .Open() ' "Provider=SQLOLEDB.1;Data Source=development", "sa", "pacesql", -1
End With

OpenSQL = db.Execute(sQuery)
End Function

End Class

我认为问题出在LoadDataBound()Sub例程中,但由于我自己无法解决这个问题,所以我无法肯定。非常感谢那些花时间阅读所有这些内容的人,如果它目前很草率并装载了未使用过的方法或变量,那么我很抱歉......我已经完成了这件事,试图让它发挥作用。

1 个答案:

答案 0 :(得分:1)

我认为问题是你使用的是ADO而不是ADO.NET。

我没有时间计算你的代码,但也许你应该从有用的东西开始。这应该从您的数据库中提取一些数据并填充datagridview。您当然需要更改连接字符串和表/字段名等:

  Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    Dim strCnn As String = "Data Source=sqlservername;Timeout=10;Database=databasename;Trusted_Connection=no;User ID=userid;Password=password;"
    Using cnn As New SqlConnection(strCnn)
      cnn.Open()
      Dim strSql As String = "SELECT * FROM Tablename"
      Dim dtb As New DataTable
      Using dad As New SqlDataAdapter(strSql, cnn)
        dad.Fill(dtb)
      End Using
      'now hide all the columns except the ones we want to show'
      dgv.AutoGenerateColumns = False 'dgv is the name of the datagridview'
      Dim intColIndex As Integer
      intColIndex = dgv.Columns.Add("Field1", "Field 1") 'aliases'
      dgv.Columns(intColIndex).DataPropertyName = "F1" 'column name in database'
      intColIndex = dgv.Columns.Add("Field3", "Field 3") 'aliases'
      dgv.Columns(intColIndex).DataPropertyName = "F3" 'column name in database'
      'now bind'
      dgv.DataSource = dtb
    End Using
  End Sub

哦,你需要

Imports System.Data
Imports System.Data.SqlClient

位于表单源代码的顶部。