none
Listview assign database but how to adjust column width RRS feed

  • Question

  • hi,

    I want to show the database values in list view,  but when it show the column are shrink.  i want column are according to data base value hader.

    my code is given below

    Imports System.Data.SqlClient
    Public Class Form2
        Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As DataSet
        Dim itemcoll(100) As String
    
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.ListView1.View = View.Details
            Me.ListView1.GridLines = True
          
            Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True")
    
            Dim strQ As String = String.Empty
    
            strQ = "SELECT Pid,Remarks,PreviousBalance,CashPaid,Remain,CustomerName,EmployeeNaame FROM dbo.PaymentReceived"
    
            cmd = New SqlCommand(strQ, conn)
            da = New SqlDataAdapter(cmd)
            ds = New DataSet
            da.Fill(ds, "Table")
    
            Dim i As Integer = 0
            Dim j As Integer = 0
    
            ' adding the columns in ListView
            For i = 0 To ds.Tables(0).Columns.Count - 1
                Me.ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
            Next
    
    
            'Now adding the Items in Listview
            For i = 0 To ds.Tables(0).Rows.Count - 1
    
                For j = 0 To ds.Tables(0).Columns.Count - 1
    
                    itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
                Next
                Dim lvi As New ListViewItem(itemcoll)
                Me.ListView1.Items.Add(lvi)
                Me.ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
    
            Next
        End Sub
    End Class


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Tuesday, January 14, 2020 7:57 PM

Answers

  • Hi,

    you can try the following method to achieve your ideas.

            For Each ch As ColumnHeader In ListView1.Columns
                ch.Width = -1 '1
                ch.Width = -2 '2
            Next
    1. Make the column width adaptive according to the column content. At this time, the column content can be guaranteed to be visible, but the column header may be collapsed.
    2. Make the column width adaptive according to the column header. At this time, you can ensure that the column header is not collapsed.

    When your column headers are longer, the second method is recommended.

    Take the following code for an example:

    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= D:\Local Database\190920.mdf"
        Dim cn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt As New DataTable
        Dim cmd As SqlCommand
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Using cn = New SqlConnection(constr)
                cn.Open()
                cmd = cn.CreateCommand()
                cmd.CommandText = "Select * From Brand"
    
    
                ' adding the columns in ListView
                sda = New SqlDataAdapter(cmd)
                sda.Fill(dt)
                For i = 0 To dt.Columns.Count - 1
                    Me.ListView1.Columns.Add(dt.Columns(i).ColumnName.ToString())
                Next
    
                'adding the Items in Listview
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                While dr.Read()
                    Dim Item As ListViewItem = New ListViewItem()
                    Item.Text = dr("brand").ToString()
                    Item.SubItems.Add(dr("description").ToString())
                    Item.SubItems.Add(dr("number").ToString())
                    ListView1.Items.Add(Item)
                End While
    
            End Using
    
            For Each ch As ColumnHeader In ListView1.Columns
                ch.Width = -1
                ch.Width = -2
            Next
        End Sub
    End Class

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Alex Li-MSFT Friday, January 17, 2020 6:01 AM
    • Marked as answer by muhammadanzar Sunday, January 19, 2020 6:10 PM
    Wednesday, January 15, 2020 2:50 AM
    Moderator

All replies

  • Usually developers want to resize by widest value in a column e.g.

    For that we use the following (see it also on my GitHub repository).


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, January 14, 2020 8:58 PM
    Moderator
  • Hi,

    you can try the following method to achieve your ideas.

            For Each ch As ColumnHeader In ListView1.Columns
                ch.Width = -1 '1
                ch.Width = -2 '2
            Next
    1. Make the column width adaptive according to the column content. At this time, the column content can be guaranteed to be visible, but the column header may be collapsed.
    2. Make the column width adaptive according to the column header. At this time, you can ensure that the column header is not collapsed.

    When your column headers are longer, the second method is recommended.

    Take the following code for an example:

    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= D:\Local Database\190920.mdf"
        Dim cn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt As New DataTable
        Dim cmd As SqlCommand
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Using cn = New SqlConnection(constr)
                cn.Open()
                cmd = cn.CreateCommand()
                cmd.CommandText = "Select * From Brand"
    
    
                ' adding the columns in ListView
                sda = New SqlDataAdapter(cmd)
                sda.Fill(dt)
                For i = 0 To dt.Columns.Count - 1
                    Me.ListView1.Columns.Add(dt.Columns(i).ColumnName.ToString())
                Next
    
                'adding the Items in Listview
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                While dr.Read()
                    Dim Item As ListViewItem = New ListViewItem()
                    Item.Text = dr("brand").ToString()
                    Item.SubItems.Add(dr("description").ToString())
                    Item.SubItems.Add(dr("number").ToString())
                    ListView1.Items.Add(Item)
                End While
    
            End Using
    
            For Each ch As ColumnHeader In ListView1.Columns
                ch.Width = -1
                ch.Width = -2
            Next
        End Sub
    End Class

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Alex Li-MSFT Friday, January 17, 2020 6:01 AM
    • Marked as answer by muhammadanzar Sunday, January 19, 2020 6:10 PM
    Wednesday, January 15, 2020 2:50 AM
    Moderator