2011-09-30 29 views
0

我有一個ComboBox,其數據源是來自SQL Server數據庫的數據集。更改與.net中的數據集關聯的組合框中的列寬度

其列的寬度是相對於列的名稱的寬度。我想增加這個寬度以便正確地看到內容而不是取消。

以下是我在談論的一個截圖:

screenshot http://uploadpic.org/storage/2011/thumb_iDSwsbVfSB4mbWvR1xNmm98Fp.png

正如你可以看到在第二列中的值不完全地出現。

這裏是我用來加載一個組合框的方法:

Public Sub CargarComboAlternativo(ByVal Combo As ComboBox, ByVal query As String) 
    Dim connectionString As String = "Data Source=" & Servidor & ";Database=" & Bdatos & ";Trusted_Connection=Yes;UID=" & UID & ";" 
    Dim adapter As SqlDataAdapter 
    Dim dataSet As DataSet = New DataSet() 

    Try 
     Using conn As SqlConnection = New SqlConnection(connectionString) 
      Using command As New SqlCommand(query, conn) 

       command.CommandType = CommandType.Text 

       conn.Open() 

       adapter = New SqlDataAdapter(command) 
       dataSet.Clear() 
       adapter.Fill(dataSet) 

       Combo.DataSource = dataSet 
      End Using 
     End Using 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 
End Sub 

有什麼建議?

我沒有在C#中的所有建議介意的話,我不會有問題,將其翻譯爲vb.net

回答

1

怎麼樣服用點狀底:

Dim total As Integer = 0 
Dim maxLen As Integer = 0 
For Each row As DataRow In ds.Tables(0).Rows 
    total = 0 
    For Each Str As String In row.ItemArray 
    total = Str.Length + total 
    Next 
    If maxLen < total Then maxLen = total 
Next 

Combo.Width = maxLen + 5 

我知道它的蠻力,但你會找到最長的項目,並設置寬度。 +5是填充你可能需要改變它。

+0

沒關係。回頭看看你的例子,我可以看到你的組合框不包含一串字符串。 –

+0

我已經更新瞭解決方案,試圖爲您指出正確的方向。我不確定轉換是否會正確地爲itemArray轉換爲字符串對象。讓我知道這是否有幫助。 –

相關問題