【问题标题】:Code to show a DataGridView works only if I run another sub before仅当我之前运行另一个子时,显示 DataGridView 的代码才有效
【发布时间】:2016-06-24 16:12:41
【问题描述】:

我编写了一个代码来将某个 DataTable 的内容显示/隐藏到 DataGridView 中。

当我单击按钮时,DataGridView 会出现和消失。 我没有收到错误,但没有显示任何内容。

但是,如果我单击另一个按钮(显示另一个表单)然后返回主表单,则代码可以正常工作。 (??!!??)

我无法理解是什么(在第二个代码中)使第一个代码起作用:似乎两个代码之间没有联系。

编辑
我做了一些测试,我可以添加更多信息:
我添加了一个按钮来显示(在 msgbox 中)DataGridView 属性。 控件已正确添加,所有属性均正确。 属性“Visible”设置为“True”,但 DataGridView 仍然“不可见”。

我添加了一个按钮来设置DGV_Tbl.Visible = FalseDGV_Tbl.Visible = True 当我单击它时,会出现 DataGridView。

但如果我再次单击 Btn_ShowHideTbl(删除 DGV)并再次单击(重新添加 DGV)DataGridView 仍然“不可见”。

当我单击按钮打开第二个表单然后关闭它以返回到第一个表单时,不会发生这种情况。
在这种情况下,一切正常。

我可以解决将DGV_Tbl.Visible = FalseDGV_Tbl.Visible = True 添加到第一个代码的问题,但我认为这不是一个好主意。
我想了解问题并在没有“奇怪指令”的情况下解决它。

编辑 2
我注意到代码 .AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells) 在不打开第二种形式的情况下也不起作用。
在这个指令上DGV_Tbl.Visible = FalseDGV_Tbl.Visible = True 没有效果。

编辑 3
我已经按照接受的答案做了,但我发布了另一个问题here,希望了解我的代码有什么问题。

这是我显示/隐藏 DataGridView 的代码:

Private Sub Btn_ShowHideTbl_Click(sender As Object, e As EventArgs) Handles Btn_ShowHideTbl.Click
    ShowHideTbl()
End Sub

Private Sub ShowHideTbl()
    'Look for DGV
    Dim DGV_Tbl As DataGridView = Nothing
    Try
        DGV_Tbl = CType(Me.Controls("DGV_Tbl"), DataGridView)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    Try
       'If not found I need to show data
       If DGV_Tbl Is Nothing Then
            If Me.CBox_ProcType.Text = "Select a Procedure" Then
                MsgBox("You need To select a Procedure", vbInformation, "Unable to show table")
                Exit Sub
            End If
            DGV_Tbl = New DataGridView
            'It needs to copy data to another DataTable to show Double as Currency
            Using DTemp As DataTable = New DataTable
                Dim TblName As String = Me.CBox_ProcType.Text
                For C As Integer = 0 To DS_All.Tables(TblName).Columns.Count - 1
                    DTemp.Columns.Add(DS_All.Tables(TblName).Columns(C).ColumnName, Type.GetType("System.String"))
                Next
                Dim Arr(DS_All.Tables(TblName).Columns.Count - 1) As String
                For R As Integer = 0 To DS_All.Tables(TblName).Rows.Count - 1
                    For C As Integer = 0 To DS_All.Tables(TblName).Columns.Count - 1
                        If C = 0 Then
                            Arr(C) = DS_All.Tables(TblName).Rows(R)(C).ToString
                        Else
                            Arr(C) = FormatCurrency(DS_All.Tables(TblName).Rows(R)(C).ToString, 2)
                        End If
                    Next
                    DTemp.Rows.Add(Arr)
                Next
                'Working on created DataGridView
                With DGV_Tbl
                    .Name = "DGV_Tbl"
                    'Add control to the Form
                    Me.Controls.Add(DGV_Tbl)
                    .DataSource = DTemp
                    .AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                    .RowHeadersVisible = False
                    .AllowUserToAddRows = False
                    .AllowUserToDeleteRows = False
                End With
            'Dispose the copied DataTable
            End Using
            'Resizing Form to include new DataGridView
            Dim DGV_H As Integer = 0
            Dim DGV_W As Integer = 0
            For Each R As DataGridViewRow In DGV_Tbl.Rows
                DGV_H += R.Height
            Next
            DGV_H += DGV_Tbl.ColumnHeadersHeight
            'Add more space to include spaces between cells
            DGV_H += CInt(DGV_Tbl.Rows.Count * 0.45)
            For Each C As DataGridViewColumn In DGV_Tbl.Columns
                DGV_W += C.Width
            Next
            'Add more space to include spaces between cells
            DGV_W += CInt(DGV_Tbl.Columns.Count * 0.45)
            DGV_Tbl.Height = DGV_H
            DGV_Tbl.Width = DGV_W
            'Resize the Form
            Me.Height += DGV_H + 30
            Me.Controls("DGV_Tbl").Location = New Point(15, Me.Height - DGV_H - 30)
            'Align for currency
            For x As Integer = 1 To DGV_Tbl.Columns.Count - 1
                DGV_Tbl.Columns(x).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            Next
        Else
            'If DGV exists I need to remove it and resize the form
            Dim DGV_H As Integer = DGV_Tbl.Height
            DGV_Tbl.Dispose()
            Me.Height -= (DGV_H + 30)
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

这是显示另一个表单的按钮中的代码(以及关闭表单并返回的代码):

Private Sub Btn_ShowSummary_Click(sender As Object, e As EventArgs) Handles Btn_ShowSummary.Click
    Try
        If Me.CBox_ProcType.Text = "Select a Procedure" OrElse Me.CBox_ProcValue.Text = "Select a Value" Then
            MsgBox("It needs to select a Procedure and a Value",
                   vbInformation, "Unable to show table")
            Exit Sub
        End If

        Summary = "...Here Some text..." & vbCrLf & Split(Me.CBox_ProcType.Text, ".")(1).Trim & vbCrLf & vbCrLf
        Summary &= "...Here Some text..." & Me.CBox_ProcValue.Text & vbCrLf & vbCrLf
        Dim C1Wdt% = -50 
        Dim C2Wdt% = TBox_TotAll.Text.Length 
        For R As Integer = 1 To 4
            Dim CBox_Phase As CheckBox = CType(Me.TLP_Phases.Controls("CBox_Phase" & R.ToString), CheckBox)
            Dim TBox_ValPh As TextBox = CType(Me.TLP_Phases.Controls("TBox_ValPh" & R.ToString), TextBox)
            If CBox_Phase.Checked Then
                Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
                                         CBox_Phase.Text, TBox_ValPh.Text) & vbCrLf
                Dim TBox_SelVarPh As TextBox = CType(Me.TLP_Phases.Controls("TBox_SelVarPh" & R.ToString), TextBox)
                If TBox_SelVarPh.Text = "" OrElse TBox_SelVarPh.Text = "€ 0,00" Then
                    Summary &= "...Here Some text..." & vbCrLf
                Else
                    Dim SelVarTxt$ = If(Val(TBox_SelVarPh.Text) > 0,
                        "...Here Some text..." & TBox_SelVarPh.Text,
                        "...Here Some text..." & TBox_SelVarPh.Text.Substring(1)) & vbCrLf
                    Summary &= SelVarTxt
                End If
            End If
        Next
        Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}", "", New String(CChar("_"), C2Wdt)) & vbCrLf
        Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
                                 "...Here Some text...",
                                 Me.TBox_TotPhases.Text) & vbCrLf
        If Me.TBox_PrtAdg.Text <> "€ 0,00" Then
            Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
                                 "...Here Some text...",
                                 Me.TBox_PrtAdg.Text) & vbCrLf
            Summary &= "...Here Some text..." & TBox_PrtRapp.Text & "...Here Some text..." & TBox_CPrt.Text & "...Here Some text..." & vbCrLf
        End If
        Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
                                 "...Here Some text..." & TBox_ForfPercent.Text,
                                 Me.TBox_ForfImp.Text) & vbCrLf
        Summary &= "...Here Some text..." & TBox_TotPhases.Text & ")" & vbCrLf
        Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}", "", New String(CChar("_"), C2Wdt)) & vbCrLf
        Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
                                 "...Here Some text...",
                                 Me.TBox_TotAll.Text) & vbCrLf
        Me.Hide()
        Me.ShowInTaskbar = False
        Frm_Summary.Show()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

这里是返回按钮的代码:

Private Sub Btn_CloseNBack_Click(sender As Object, e As EventArgs) Handles Btn_CloseNBack.Click
    Frm_Base.ShowInTaskbar = True
    Frm_Base.Show()
    Me.Close()
End Sub

我没有看到代码之间有任何联系(但似乎我错了)请告诉我我缺少什么。

【问题讨论】:

  • 您是否尝试在您的Btn_CloseNBack_Click() 中放置一个断点并在Debug 中运行?
  • @Supersnake 我试过了。仅当我运行所有第二个代码时,第一个代码才有效
  • 很可能很多问题都与使用默认表单实例和切换ShowInTaskbar有关。创建动态控件会带来成本,并且有责任在完成后处理它们。所有这些都比切换 Visible 属性更“奇怪”。

标签: vb.net controls


【解决方案1】:

我可以为您的问题建议另一种方法吗? 如果我是你,我会考虑使用绘制的 datagridview,事先将数据表内容放入其中。 然后在显示/隐藏按钮中,只需切换该 datagridview 的可见性以及重新计算表单大小。

对于货币列,您可以将数据表填充到datagridview中,然后将该列的格式设置为货币,而不是制作另一个表:

DGV_Tbl.Columns("CurrencyColumn").DefaultCellStyle.Format = "c"

关于datagridview列格式的更多信息可以找到here

【讨论】:

  • 我不知道发生了什么,但是当删除将数据从 DataTable 复制到另一个的代码时,我更改了 DGV_Tbl.Datasource 一切正常。谢谢! +1 单元格格式
  • 糟糕。由于DGV_Tbl.Visible = FalseDGV_Tbl.Visible = True,它起作用了。我知道我可以改变方法,但我想在我的代码中找到错误
  • 当您单击按钮时,虽然 datagridview 没有出现,但表单是否按预期调整大小?我想为你的“if”情况在两行中添加断线 - 你期望 datagridview 存在的地方 - 以及你期望它不存在的地方。看看对应的 if 是不是像你想象的那样被调用?
  • 是的,表格大小已正确调整,所有 DGV 单元格均已正确填写。只有可见性和列自动调整大小不起作用。
  • 如你所说,只有在打开和关闭第二个表单后才会出现datagridview。那么如果您将 Me.Hide() Me.Show() 作为显示 datagridview 的“if”块中的最后一个命令并调整表单大小会发生什么?会出现datagridview吗?如果不是,那么问题出在您的 form2 代码中。检查 Form2_Load 以查看是否可以弄清任何内容。否则,如果它出现,那么很明显这个问题是由你的表单从不可见切换到可见的方式引起的,因为 Form.Hide() 等于 Form.Visible = true。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
相关资源
最近更新 更多