如何在vb.net的datagridview中显示leftside上的fieldsname及其右侧的值?

时间:2016-02-02 11:38:29

标签: vb.net

 Private Sub OpenFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileToolStripMenuItem.Click

    Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.InitialDirectory = "c:\"
    openFileDialog1.Filter = "mdb files (*.mdb)|*.mdb|All files (*.*)|*.*"
    openFileDialog1.FilterIndex = 1
    openFileDialog1.RestoreDirectory = True


    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                myConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & openFileDialog1.FileName
                Dim cmd As New OleDb.OleDbCommand("SELECT Image001 FROM [DDCOS] WHERE RecNum = @RecNum", con)
                con.ConnectionString = myConString



                Dim path As New IO.DirectoryInfo(openFileDialog1.FileName)
                Dim childLen As Integer = path.Name.Length 
                Dim parentLen As Integer = path.Parent.Name.Length
                Dim path2 As String = openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.Length - (childLen + parentLen + 1)) & "Images"  
  con.Open()

                datagridshow()
            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open. 

            If (myStream IsNot Nothing) Then
                myStream.Close()
            End If
        End Try
    End If

End Sub

Private Sub CARD_DECK_ENTRY_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    con.ConnectionString = myConString
    con.Open()
    datagridshow()
End Sub

Private Sub datagridshow()
    Dim ds As New DataSet
    Dim dt As New DataTable
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter
    Dim ODDA As New OleDbDataAdapter

    da = New OleDbDataAdapter("Select * from Data001", con)
    da.Fill(dt)

    DataGridView1.DataSource = dt.DefaultView

    con.Close()

End Sub

上面的代码是我浏览mdb的代码,并在datagrid上输入它的值。我正打算在左侧显示字段名称,在右侧显示它的值。该怎么办?

1 个答案:

答案 0 :(得分:0)

根据我的问题,

  1. 您需要创建一个新的datatable
  2. column中添加所需数量datatable的名称。
  3. 迭代地将新row添加到datatable
  4. 使用row column填充datatable OleDb中填充的数据的新Query
  5. 然后bind这个新datatabledatagridviewdatasource
  6. 这可能会对你有帮助。