DataGrid查看Vb.Net - 填充数据

时间:2016-10-24 16:57:03

标签: vb.net datatable datagrid

我有以下代码,我一直在一起做我想做的事情。基本上我需要从SQL Server中选择数据,得到记录的计数,我将在标签中显示(现在它在msgbox中)然后我希望能够用数据中的数据填充列0,1表。我之所以指定列是因为我试图确保一切正常,因为我将会选择更多的列,但我会隐藏其中的一些(只需要它们的值)。这是我的代码看起来像......

    Dim count As Int16
    Dim queryString As String = "select clientID, Dob, SSN from tblClients"
    Dim cmd As New SqlCommand(queryString, Conn)
    Conn.Open()
    Dim reader As SqlDataReader = cmd.ExecuteReader()
    Try
        If reader.Read() Then
            While reader.Read
                count = count + 1
            End While
            MsgBox(count)
        Else
            MsgBox("No results were found for the criteria you've select, please update your selection and try again", MsgBoxStyle.OkOnly, "No results found....")
        End If
    Finally
        reader.Close()
        exit sub

    End Try
    Using sdA As New SqlDataAdapter
        Using dtA As New DataTable()
            sdA.SelectCommand = cmd
            sdA.Fill(dtA)
            grdFind.AutoGenerateColumns = False
            grdFind.ColumnCount = 2

            grdFind.Columns(0).Name = "clientid"
            grdFind.Columns(0).HeaderText = "Clientid"
            grdFind.Columns(0).DataPropertyName = "ClientID"

            grdFind.Columns(1).Name = "dob"
            grdFind.Columns(1).HeaderText = "dob"
            grdFind.Columns(1).DataPropertyName = "dob"

            grdFind.DataSource = dtA(0)
        End Using
    End Using

我正在按下按钮点击.....我以前尝试过直接从它工作的数据表加载数据,但是当我指定列时它不会。你知道为什么?目前,我在网格中看到的只是标题文本。反正有没有让它更有效率?

编辑: enter image description here

0 个答案:

没有答案