我有一个ComboBox,其数据源是来自SQL Server数据库的数据集。
其列的宽度相对于列名称的宽度。我想增加这个宽度,以便正确地看到内容而不是诅咒。
以下是我所说的截图:
screenshot http://uploadpic.org/storage/2011/thumb_iDSwsbVfSB4mbWvR1xNmm98Fp.png
如您所见,第二列中的值不会完整显示。
以下是我用来加载ComboBox的方法:
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没有问题
答案 0 :(得分: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用于填充,您可能需要更改它。