2012-06-19 106 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的繪製「透明」控件的方式相比,我的代碼更多。但這裏也應該是避免/掩蓋/隱藏這些問題的一種方法。歡迎任何進一步的想法。 –

相關問題