更改与 .net 中的数据集关联的组合框中的列宽

发布于 2024-12-07 12:09:12 字数 1223 浏览 1 评论 0原文

我有一个 ComboBox,其数据源是来自 SQL Server 数据库的数据集。

其列的宽度与列名称的宽度相关。我想增加宽度,以便正确查看内容而不是被剪切。

这是我正在谈论的内容的屏幕截图:

screenshot

正如您所看到的,第二列中的值并未完全显示。

这是我用来加载组合框的方法:

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 不会有问题

I have a ComboBox with its datasource being a dataset from a SQL Server database.

The width of its columns are relative to the width of the name of the columns. I would like to increase that width in order to see the content properly and not cuted.

Here is a screenshot of what I'm talking about:

screenshot

As you can see the values in the second column do not appear completly.

Here is the method I use to load a 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

Any suggestions?

I don't mind at all a suggestion in C#, I won't have problems to translate it to vb.net

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆依然 2024-12-14 12:09:12

最后怎么样:

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 用于填充,您可能需要更改它。

what about somehting like at the end:

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

I know its brute force but you'll find the longest item and set the width to that. the +5 is for padding you may need to change that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文