Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 调整datagridview的大小和重画_Vb.net - Fatal编程技术网

Vb.net 调整datagridview的大小和重画

Vb.net 调整datagridview的大小和重画,vb.net,Vb.net,我使用datagridview显示来自数据库的查询结果,该数据库的行数可能为0到x。 所以我进行了计算,以计算参考底图表单的大小,并且我的datagridview依赖于匹配行的数量。 底层表单是透明的,所有这些看起来都像是用户控制显示和工作正常的内容 但有一个问题: 每当datagrid必须增长时,在datagrid被填充之前,该区域中的黑色方块就会显示出来,这不是好事,肯定是不需要的 我能做些平常的事来避免这种情况吗? datagridview是否有某种机制在填充完成时对其进行刷新并显示数据?

我使用datagridview显示来自数据库的查询结果,该数据库的行数可能为0到x。
所以我进行了计算,以计算参考底图表单的大小,并且我的datagridview依赖于匹配行的数量。
底层表单是透明的,所有这些看起来都像是用户控制显示和工作正常的内容

但有一个问题:
每当datagrid必须增长时,在datagrid被填充之前,该区域中的黑色方块就会显示出来,这不是好事,肯定是不需要的

我能做些平常的事来避免这种情况吗?
datagridview是否有某种机制在填充完成时对其进行刷新并显示数据?
或者还能做什么

   Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    Dim tw As Integer = 0
    Dim n As Integer = 0
    Dim sqlText As String
    Dim reader As OdbcDataReader = Nothing
    Dim btCommand As OdbcCommand = Nothing
    Dim mCmd As OdbcCommand = Nothing
    Dim mCon As New Odbc.OdbcConnection

    DataGridView1.Rows.Clear()
    If Trim(TextBox1.Text).Length Then
        mCon.ConnectionString = "Dsn=" + dbDsn + _
                                ";database=" + mydatabase + ";server=" + dbServer + ";port=" + dbPort + _
                                ";uid=" + dbUser + ";pwd=" + dbPass
        Try
            mCon.Open()
            btCommand = New OdbcCommand("BEGIN TRANSACTION", mCon)
            sqlText = "SELECT dtbl_id, name... etc... FROM mytable WHERE name ILIKE '%" & Trim(TextBox1.Text) & "%' ORDER BY name LIMIT 128"
            mCmd = New OdbcCommand(sqlText, mCon)
            reader = mCmd.ExecuteReader()
            While (reader.Read())

                Dim t_kol, t_ci As String
                If reader.GetValue(4) - reader.GetValue(5) = 0 Then
                    t_kol = "- "
                Else
                    t_kol = FormatNumber((reader.GetValue(4) - reader.GetValue(5)), 2)
                End If
                t_ci = FormatNumber(reader.GetValue(3))

                With DataGridView1.Rows.Add(New String() {reader.GetValue(0).ToString(), _
                                                          reader.GetValue(1).ToString(), _
                                                          reader.GetValue(2).ToString(), _
                                                          t_kol, _
                                                          t_ci, _
                                                          reader.GetValue(6).ToString(), _
                                                          reader.GetValue(7).ToString})
                    n = n + 1
                End With
            End While
            btCommand = New OdbcCommand("END TRANSACTION", mCon)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        Finally
            reader.Close()
            reader.Dispose()
            btCommand.Dispose()
            mCmd.Dispose()

            If n < 1 Then
                DataGridView1.Height = 0
                DataGridView1.Height = DataGridView1.Height + DataGridView1.Top
            End If

        End Try
        mCon.Close()
        mCon.Dispose()
    End If

    If n < 1 Then
        DataGridView1.Height = 0
    Else
        DataGridView1.Height = DataGridView1.ColumnHeadersHeight + (n * DataGridView1.RowTemplate.Height) + 2
    End If

    Dim p As Point = Me.PointToScreen(DataGridView1.Location)
    If p.Y + DataGridView1.Height >= My.Computer.Screen.WorkingArea.Height Then DataGridView1.Height = My.Computer.Screen.WorkingArea.Height - p.Y

    tw = totalwidth + (Math.Abs(CInt(DataGridView1.Controls(1).Visible)) * CInt(DataGridView1.Controls(1).Width))
    DataGridView1.Width = tw + 2
    With Me
        .Width = tw + 4
        .Height = DataGridView1.Height + DataGridView1.Top
    End With
End Sub
Private Sub TextBox1\u TextChanged(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理TextBox1.TextChanged
Dim tw作为整数=0
尺寸n为整数=0
将sqlText设置为字符串
作为OdbcDataReader的Dim读卡器=无
Dim btCommand作为OdbcCommand=无
Dim mCmd As OdbcCommand=无
Dim mCon作为新的Odbc.Odbc连接
DataGridView1.Rows.Clear()
如果修剪(TextBox1.Text).Length则
mCon.ConnectionString=“Dsn=“+dbDsn+_
“database=“+mydatabase+”;server=“+dbServer+”;port=“+dbPort+_
“uid=“+dbUser+”pwd=“+dbPass
尝试
mCon.Open()
btCommand=新的OdbcCommand(“开始事务”,mCon)
sqlText=“从mytable中选择dtbl\u id、名称…等,其中名称类似“%”和Trim(TextBox1.Text)和“%”按名称排序限制128”
mCmd=新的OdbcCommand(sqlText,mCon)
reader=mCmd.ExecuteReader()
While(reader.Read())
Dim t_kol,t_ci作为字符串
如果reader.GetValue(4)-reader.GetValue(5)=0,则
t_kol=“-”
其他的
t_kol=FormatNumber((reader.GetValue(4)-reader.GetValue(5)),2)
如果结束
t_ci=FormatNumber(reader.GetValue(3))
使用DataGridView1.Rows.Add(新字符串(){reader.GetValue(0).ToString()_
reader.GetValue(1.ToString()_
reader.GetValue(2.ToString()_
图科尔_
杜慈_
reader.GetValue(6.ToString()_
reader.GetValue(7.ToString})
n=n+1
以
结束时
btCommand=新的OdbcCommand(“结束事务”,mCon)
特例
MsgBox(例如Message,MsgBoxStyle.Critical+MsgBoxStyle.OkOnly)
最后
reader.Close()
reader.Dispose()
btCommand.Dispose()
mCmd.Dispose()
如果n<1,则
DataGridView1.Height=0
DataGridView1.Height=DataGridView1.Height+DataGridView1.Top
如果结束
结束尝试
mCon.Close()
mCon.Dispose()
如果结束
如果n<1,则
DataGridView1.Height=0
其他的
DataGridView1.Height=DataGridView1.ColumnHeadershight+(n*DataGridView1.RowTemplate.Height)+2
如果结束
尺寸p作为点=Me.PointToScreen(DataGridView1.Location)
如果p.Y+DataGridView1.Height>=My.Computer.Screen.WorkingArea.Height,则DataGridView1.Height=My.Computer.Screen.WorkingArea.Height-p.Y
tw=totalwidth+(Math.Abs(CInt(DataGridView1.Controls(1.Visible))*CInt(DataGridView1.Controls(1.Width))
DataGridView1.Width=tw+2
和我一起
.宽度=tw+4
.Height=DataGridView1.Height+DataGridView1.Top
以
端接头

我不知道你是如何编写你的东西的,但是你有没有试着把

DataGridView1.DataSource = someList
DataGridView1.DataBind()
If DataGridView.Rows.Count > 0 Then
    Dim p As Point = Me.PointToScreen(DataGridView1.Location)
    If p.Y + DataGridView1.Height >= My.Computer.Screen.WorkingArea.Height Then
        DataGridView1.Height = My.Computer.Screen.WorkingArea.Height - p.Y
        tw = totalwidth + (Math.Abs(CInt(DataGridView1.Controls(1).Visible)) * CInt(DataGridView1.Controls(1).Width))
        DataGridView1.Width = tw + 2
    With Me
        .Width = tw + 4
        .Height = DataGridView1.Height + DataGridView1.Top
    End With
End If
End Sub
用你的方法

正如我所说,我不知道你是如何编写你的程序的,所以不要误会我,或者如果你觉得它很愚蠢,也不要麻烦……)

编辑:我更改了代码。明白我的意思了吧,如果我不是很清楚的话,很抱歉


问题是,您应该只在将一些行添加到DataGridView之后才绘制您的点,因此我将您的一些代码放在If块中。不要打扰我的数据源和数据绑定部分,我只是想澄清If块的要点。

仍然无法获得解决方案。即使是后面的白色方块而不是黑色方块?你的代码和我的完全一样。经过多次尝试,我得出结论,这种奇怪的行为更多地与windows绘制“透明”控件的方式有关,而不是与我的代码有关。但这里也应该有一种避免/掩盖/隐藏这些问题的方法。欢迎有任何进一步的想法。