2012-06-19 107 views
0

我使用datagridview显示来自数据库的查询结果,该结果可能有0到x行数。
所以我做了计算,以计算底层窗体的大小和我的datagridview可以匹配的行数。
基础形式是透明的,所有这些看起来像用户控件显示和工作得很好。调整大小并重新绘制datagridview

但是,这里有一个问题:
每次数据网格必须增长时,该区域中的黑色方块显示在填充数据网格之前,什么是不好的,肯定是不需要的。

我可以做一些平常的事情来避免这种情况吗?
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 

回答

0

我不知道你是怎么做的代码你的事,但你尝试把像

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块中。不要打扰我的DataSource和DataBind部分,我只是想澄清我的If块的重点。

+0

我现在添加了我的代码,如果意味着什么。 –

+0

仍然无法获得解决方案。甚至是白色的方块而不是黑色的? –

+0

您的代码与我的代码完全相同。经过多次尝试后,我不得不得出结论,这种奇怪的行为更多地与Windows的绘制“透明”控件的方式相比,我的代码更多。但这里也应该是避免/掩盖/隐藏这些问题的一种方法。欢迎任何进一步的想法。 –

相关问题