动态添加的控件 (DataGridView) 无法正常工作 [英] Dynamically added control (DataGridView) doesn't work properly

查看:40
本文介绍了动态添加的控件 (DataGridView) 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布了一个问题 在这里,经过多次尝试,我接受了建议我使用隐藏控件而不是添加控件的答案.

I posted a question here and, after many attempts, I accepted the answer who suggested me to use an hidden control instead of an added control.

这样我的代码可以正确运行,但我想了解错误.

This way my code run correctly but I would like to understand the mistake.

事情是这样的:当我按下一个按钮时,会添加一个新的 DataGridView 但是
a) 即使他的可见属性设置为 True,它也不可见;
b) 如果(当添加的 DGV 在表单上时)我将 DGV 属性设置为 False 然后我将其重新设置为 True DGV 出现但列未调整大小;
c) 如果我删除添加的 DGV 并重新添加它,它再次不可见".

That's what happens: When I press a button a new DataGridView is added but
a) it isn't visible even if his property visible is set True;
b) if (while the added DGV is on the form) I set DGV property visible to False and then I re-set it to True the DGV appears but columns aren't resized;
c) if I remove the added DGV and re-add it, it is "invisible" again.

如果我运行另一个 SUB,所有这些问题都不会发生

ALL THESE TROUBLES DON'T HAPPEN IF I RUN ANOTHER SUB

如果:
a) 我运行了一个隐藏我的第一个表单并显示另一个表单的子程序;
b) 然后我关闭第二个表单以返回到第一个表单;
一切正常:
DGV 已正确添加;
它是可见的;
所有列都正确调整大小;
如果我移除 DGV 或重新添加它,一切仍然有效.

If:
a) I run a sub that hides my first form and shows another form;
b) then I close the 2nd form to go back to the first form;
all works fine:
the DGV is correctly added;
it is visible;
all columns are correctly resized;
if I remove the DGV or re-add it, all still works.

我哪里错了?

推荐答案

这是我从您之前的链接中获得的代码.它在我的表单中按预期显示了 datagridview.如您所见,这正是您的代码,除了使用"块 --- 以及您检索数据表的方式.这是唯一的两个区别.

This is the code I got from your earlier link. It shows the datagridview as expected in my form. As you can see, it is exactly your code, with the exception of that "using" block --- and the way you retrieve the datatable. That's the only two differences.

Private Sub ShowHideTbl()
    Dim DTemp As DataTable = GetTable()
    Dim DGV_Tbl As DataGridView = Nothing
    Try
        DGV_Tbl = CType(Me.Controls("DGV_Tbl"), DataGridView)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    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
    End If
    DGV_Tbl = New DataGridView
    With DGV_Tbl
        .Name = "DGV_Tbl"
        .DataSource = DTemp
        Me.Controls.Add(DGV_Tbl)
        .AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        .RowHeadersVisible = False
        .AllowUserToAddRows = False
        .AllowUserToDeleteRows = False
    End With
    Dim DGV_H As Integer = 0
    Dim DGV_W As Integer = 0
    For Each S As DataGridViewRow In DGV_Tbl.Rows
        DGV_H += S.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 S As DataGridViewColumn In DGV_Tbl.Columns
        DGV_W += S.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)
End Sub

你的问题很奇怪,因为我没有代码来尝试和测试,我唯一的建议是你把这段代码粘贴到一个新的表单中,看看它是否有效.如果是,则将表单1的其他控件添加到其中,再次运行代码,看看它是否仍然有效.重复此过程,直到找出原因.

Your problem is most strange, and since I don't have the code to try and test, my only suggest is that you paste this code into a new form, see if it works. If it does, then add your other control of form 1 into it, run the code again, see if it still work. Repeat the process until you find out what is the cause.

我相信您可以通过此了解您的表单中真正发生了什么.谁知道呢,这种新形式可能会奏效.

I believe you can find out what is really happening in your form with this. Who know, that new form will probably work.

这篇关于动态添加的控件 (DataGridView) 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆