2011-09-30 47 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转换为字符串对象。让我知道这是否有帮助。 –