使用VB.Net,如何控制单个DataGridView Header单元格的可见性

问题描述 投票:0回答:1

社区,

我正在开发一个将使用 DatGridView 对象的应用程序。我找不到隐藏复选框列的列标题文本的属性或方法。

我尝试过的属性包括:DataGridView、DataGridViewColumn、DataGridViewColumn.HeaderCell、DataGridViewColumn.HeaderText、DataGridViewColumnHeaderCell、DataGridViewTopLeftHeaderCell

使用 Visual Studio 2022 进行调试时,报告 myDGV 包含 0 列,但 datagridview 在表单上可见。我很困惑。

总结一下代码:

  • 建立一个名为myTable的数据表
  • 创建一个名为 myView 的数据视图
  • myView = myTable.DefaultView
  • 创建一个名为 myDGV 的 DataGridView
  • myDGV.DataSource = myView

也许我从另一个人借来的这段代码可以解释为什么我无法隐藏第一个标题单元格中的文本“CheckBoxCol”。

AddReference "System.Data"
AddReference "System.Drawing"
AddReference "System.Xml"
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms

Public Class FormClass

    ' Form, data table, data view, data grid view, string, integers, property sets
    Public myForm As New Form
    Public myTable As New DataTable
    Public myView As DataView
    Public WithEvents myDGV As New DataGridView
    Public WithEvents btnDone As New Button

    Function BuildBaseTable() As DataTable
        ' Add columns
        myTable.Columns.Add("CheckBoxCol", GetType(Boolean))
        myTable.Columns.Add("Sheet", GetType(String))
        myTable.Columns.Add("Note", GetType(String))
        ' Hide columns
        myTable.Columns("Sheet").ColumnMapping = MappingType.Hidden
        ' Add rows
        myTable.Rows.Add(False, "HEAD",         "ANGLE END FLANGES TO BE SKIP/CONTINUOUSLY WELDED. [one or the other]")
        myTable.rows.add(False, "TAIL",         "ANGLE END FLANGES TO BE SKIP/CONTINUOUSLY WELDED. [one or the other]")
        myTable.rows.add(False, "INTERMEDIATE", "ANGLE END FLANGES TO BE SKIP/CONTINUOUSLY WELDED. [one or the other]")
        ' Return the updated table
        Return myTable
    End Function

    Public Sub btnDone_Click(ByVal sender As System.Object, ByVal E As System.EventArgs)
        myForm.Close
    End Sub

    Public Sub Main()

        ' Format the Done button
        btnDone = New Button
        With btnDone
            .Name = "btnDone"
            .Location = New System.Drawing.Point(50, 50)
            .Text = "Done"
            .AutoSize = True
            .AutoSizeMode = AutoSizeMode.GrowAndShrink
            AddHandler.Click, AddressOf btnDone_Click
        End With

        'Build the basic data table
        BuildBaseTable

        'Create a DataView based on the DataTable
        myView = myTable.DefaultView
        
        ' Sort the data
        myView.Sort = "Sheet"

        ' Format the DataGrid column headers
        With myDGV.ColumnHeadersDefaultCellStyle
            .BackColor = System.Drawing.Color.Navy
            .ForeColor = System.Drawing.Color.White
            .Font = New Font(myDGV.Font, FontStyle.Bold)
        End With

        ' Format the remaining DataGrid properties
        With myDGV
            ' Set property values appropriate for read-only display and limited interactivity. 
            .AutoGenerateColumns = True 'False
            .AutoSize = True
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
            .Dock = DockStyle.Fill
            .RowHeadersVisible = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = True
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
            .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
            .EnableHeadersVisualStyles = False
            .MultiSelect = True
            .ReadOnly = True
            .RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
            .SelectionMode = DataGridViewSelectionMode.CellSelect ' .FullRowSelect

            ' Set selection background color for all the cells.
            .DefaultCellStyle.SelectionBackColor = System.Drawing.Color.DeepSkyBlue
            .DefaultCellStyle.SelectionForeColor = System.Drawing.Color.Black

            ' Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
            ' value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
            .RowHeadersDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Empty

            ' Set background color for all rows and for alternating rows. 
            ' Value for alternating rows overrides the value for all rows. 
            '.RowsDefaultCellStyle.BackColor = System.Drawing.Color.AntiqueWhite
            '.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.LightGreen

            ' Set grid color
            .GridColor = System.Drawing.Color.Blue

            ' Set data source
            .DataSource = myView
            '.DataMember = "TableName"          

        End With

        'Format the UserForm
        With myForm
            ' Set up form
            .formborderstyle = FormBorderStyle.Sizable
            '           .FormBorderStyle = FormBorderStyle.FixedToolWindow
            .StartPosition = FormStartPosition.CenterScreen
            .Width = 600
            .Height = 200
            .TopMost = True
            .Text = "Add Drawing Note"
            .Name = "Custom Form 1"
            .IsMdiContainer = False

            'Add controls
            .Controls.Add(myDGV)        'Data grid
            .Controls.Add(btnDone)      'Done button

        End With

        myForm.ShowDialog()

    End Sub

End Class

我删除了尝试使用上述属性的行,因此需要添加可能的代码来控制单个单元格的可见性。我可以提供我尝试过的代码和错误,但由于 myDGV columns=0,我认为所有错误都不会没有价值。

解决此问题后,我可以进行复选框切换并在用户关闭表单时检查其状态。改天再发一篇文章。

感谢您的时间和关注。我期待您的回复。

问候, 杰瑞

我尝试编辑这些 myDGV 属性,但由于 myDGV 似乎没有列,所以报告了所有错误。

  • 数据网格视图
  • 数据网格视图列
  • DataGridView.HeaderCell.Value
  • DataGridViewColumn.HeaderCell
  • DataGridViewColumn.HeaderText
  • DataGridViewColumnHeaderCell
  • DataGridViewTopLeftHeaderCell

例如,以下代码不会进入循环,因为 myDGV 列计数为 0:

For Each column As DataGridViewColumn In myDGV.Columns
    MsgBox("Value: " & Column.HeaderCell.Value & vbLf & _
        "Index: " & Column.Index.ToString)
Next

此代码报告超出范围:

vb.net datagridview header cell dataview
1个回答
0
投票

您面临的问题是,datagridview 仅在调用 showdialog 并运行或启动更好时才存在。

当您从 myForm 捕获 Onload 时,您仍然可以更改文本

首先给datagridview一个表单的唯一名称

    With myDGV
        ' Set property values appropriate for read-only display and limited interactivity. 
        .AutoGenerateColumns = True 'False
        .AutoSize = True
        .Name = "MyDgv"  '<-- add name
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

然后添加事件处理程序

        'Add controls
        .Controls.Add(myDGV)        'Data grid
        .Controls.Add(btnDone)      'Done button

    End With
    AddHandler myForm.Load, AddressOf myForm_Load '<-- event handler
    myForm.ShowDialog()

并添加事件处理程序的子部分

Private Sub myForm_Load(sender As Object, e As EventArgs)

    For ix As Integer = Application.OpenForms.Count - 1 To 0 Step -1
        Dim frm = Application.OpenForms(ix)
        If frm.Name = "Custom Form 1" Then
            Dim dgv As DataGridView = CType(frm.Controls("MyDgv"), DataGridView)
            dgv.Columns(0).HeaderText = "Mycheckbox"
        End If
    Next
End Sub

你得到的结果

© www.soinside.com 2019 - 2024. All rights reserved.