只有选定的单元格在数据网格中可见

时间:2014-02-19 03:25:15

标签: .net vb.net datagridview

我目前遇到有关在datagrid中查看数据的问题。我能够在datagridview中显示它,但问题是数据是不可见的。只有在选中时才会显示。

这是输出显示:

enter image description here

enter image description here

我只想知道我可以将哪些命令添加到我的代码中以使其可见。谢谢。这是我的代码:

    Imports System.Data.OleDb

Public Class ProfessorList
    Private Sub ProfessorList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvView()
    End Sub

    Private Sub dgvView()
        Using conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database.accdb;")
            conn.Open()
            Dim command As New OleDbCommand("SELECT * from ProfessorListTable", conn)
            Dim adapter As New OleDbDataAdapter
            Dim dt As New DataTable
            adapter.SelectCommand = command
            adapter.Fill(dt)
            dgvProfessorList.DataSource = dt
            addDGV()
            adapter.Dispose()
            command.Dispose()
            conn.Close()
        End Using
    End Sub

    Public Sub addDGV()
        With dgvProfessorList
            .Columns(0).Width = 100
            .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(1).Width = 150
            .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
            .Columns(2).Width = 150
            .Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
            .Columns(3).Width = 40
            .Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
            .Columns(4).Width = 100
            .Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
            .Columns(5).Width = 110
            .Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        End With
    End Sub

    Private Sub btnFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        Try
            Using conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database.accdb;")
                conn.Open()
                Dim query As String = "SELECT * FROM ProfessorListTable "
                Dim where As String = ""
                If rbEngineering.Checked Then where &= " Department = 'Engineering' AND"
                If rbArchitecture.Checked Then where &= " Department = 'Architecture' AND"
                If rbTechnology.Checked Then where &= " Department = 'Technology' AND"
                If where <> "" Then query &= "WHERE" & where
                query = query.Substring(0, Len(query) - 3)

                Dim cmd As New OleDbCommand(query, conn)
                Dim adapter As New OleDbDataAdapter
                Dim dt As New DataTable
                adapter.SelectCommand = cmd
                adapter.Fill(dt)
                dgvProfessorList.DataSource = dt
                addDGV()
                adapter.Dispose()
                cmd.Dispose()
                conn.Close()
            End Using
        Catch
            MsgBox("Please select Department to filter!", MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProfile.Click
        With Profile
            .lblIDnum.Text = dgvProfessorList.CurrentRow.Cells(0).Value
            .lblFirstName.Text = dgvProfessorList.CurrentRow.Cells(2).Value
            .lblInitial.Text = dgvProfessorList.CurrentRow.Cells(3).Value
            .lblLastName.Text = dgvProfessorList.CurrentRow.Cells(1).Value
            .lblDept.Text = dgvProfessorList.CurrentRow.Cells(4).Value
            .lblYearEmployed.Text = dgvProfessorList.CurrentRow.Cells(5).Value
            .ShowDialog()
        End With
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

检查DataGridViewCells是否没有应用样式,其中ForeColor与BackColor相同。

检查DataGridView.DefaultCellStyle属性。

干杯