显示DataGridView的代码只有在我之前运行另一个sub时才有效

时间:2016-06-24 16:12:42

标签: vb.net controls

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

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

但是如果我点击另一个按钮(显示另一个表单)然后返回主表单,代码就可以了。 (?? !! ??)

我无法理解(在第二个代码中)使第一个代码工作的原因:似乎两个代码之间没有连接。

修改
我做了一些测试,我可以添加更多信息:
我添加了一个按钮来显示(在msgbox中)DataGridView属性。 控件已正确添加,所有属性都正确。 该物业"可见"已设置" 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

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

1 个答案:

答案 0 :(得分:1)

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

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

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

有关datagridview列格式的更多信息,请访问here