Hello,


Can you help me please im trying to use the listview but i found difficulties on it.I don't know how to display my column header in my listview.Please help me.Thank you in advance hoping for your positive response.

Here is my code.

Option Explicit

Private Sub Form_Load()
 
 ConnectToDB
 ColumnName
End Sub
Private Sub ColumnName()

 With lvwStud
   .ColumnHeaders.Clear
   .ColumnHeaders.Add , , "First Name"
   .ColumnHeaders.Add , , "Last Name"
   .ColumnHeaders.Add , , "Address"
   
   
  End With

End Sub

Try This :

Option Explicit

Private Sub Form_Load()
 ConnectToDB
 Call ColumnName(lvwStud, "First Name", 40, "Last Name", 40, "Address", 70)
End Sub

Private Sub ColumnName(LSV As ListView, ParamArray lstview())
    Dim i, width
    LSV.View = lvwReport
    width = LSV.width - 80
    With LSV.ColumnHeaders
        .Clear
        For i = 0 To UBound(lstview) - 1 Step 2
        .Add , , lstview(i), (lstview(i + 1) * width) / 100
        Next i
    End With
End Sub

Try This :

Option Explicit

Private Sub Form_Load()
 ConnectToDB
 Call ColumnName(lvwStud, "First Name", 40, "Last Name", 40, "Address", 70)
End Sub

Private Sub ColumnName(LSV As ListView, ParamArray lstview())
    Dim i, width
    LSV.View = lvwReport
    width = LSV.width - 80
    With LSV.ColumnHeaders
        .Clear
        For i = 0 To UBound(lstview) - 1 Step 2
        .Add , , lstview(i), (lstview(i + 1) * width) / 100
        Next i
    End With
End Sub

Hello sir thank you for the reply, I have some question sir what reference did you use with this sir? why did you use this and you use the call in the formload...can you please put some comments sir so that i can understand what is the purpose for this.

Private Sub ColumnName(LSV As ListView, ParamArray lstview())//this one sir
    Dim i, width
    LSV.View = lvwReport //this one sir
    width = LSV.width - 80 //this one sir
    With LSV.ColumnHeaders
        .Clear//here sir why is it clear
        For i = 0 To UBound(lstview) - 1 Step 2//what is UBound sir?
        .Add , , lstview(i), (lstview(i + 1) * width) / 100//also here sir i could not understand 
        Next i
    End With

End Sub

Please help me to enligthen my mind sir.Thank you in advance hoping for your positive response.

why did you use this and you use the call in the formload

I give you this code to make you more easier when you set a listview header. You can use this procedure at any event. Call used to call subroutine like procedure or function.

can you please put some comments sir so that i can understand what is the purpose for this.

Okay, I'll try to explain it.

Private Sub ColumnName(LSV As ListView, ParamArray lstview()) ' LSV declared as listview , lstview is an array to receive column name n width (see the paramaters when this procedure called in form load)
    Dim i, width
    LSV.View = lvwReport 'this will make listview looks like report view.
    width = LSV.width - 80 ' set the width
    With LSV.ColumnHeaders
        .Clear ' Clear the listview header before add headers.
        For i = 0 To UBound(lstview) - 1 Step 2 ' UBound used to get last index of an  array, step 2 is forward 2 steps (i=0,i=2,i=4,i=6...)
        .Add , , lstview(i), (lstview(i + 1) * width) / 100 ' lstview(i) is data on array at index i. lstview(i + 1) is data on array at index i + 1.
        ' Why is i + 1? cause in this looping we use step 2, so if index i is header name then index (i + 1) is header width.
        ' i=0 is header name, i=1 is header width, i=2 is header name, i=3 is header width, and so on.
        ' Call ColumnName(lvwStud, "First Name", 40, "Last Name", 40, "Address", 70)
        ' you can see that "first name' is at index i (start from 0), at index i + 1 is 40 (0 + 1), and so on.
        Next i
    End With

End Sub

I give you this code to make you more easier when you set a listview header. You can use this procedure at any event. Call used to call subroutine like procedure or function.

Okay, I'll try to explain it.

Private Sub ColumnName(LSV As ListView, ParamArray lstview()) ' LSV declared as listview , lstview is an array to receive column name n width (see the paramaters when this procedure called in form load)
    Dim i, width
    LSV.View = lvwReport 'this will make listview looks like report view.
    width = LSV.width - 80 ' set the width
    With LSV.ColumnHeaders
        .Clear ' Clear the listview header before add headers.
        For i = 0 To UBound(lstview) - 1 Step 2 ' UBound used to get last index of an  array, step 2 is forward 2 steps (i=0,i=2,i=4,i=6...)
        .Add , , lstview(i), (lstview(i + 1) * width) / 100 ' lstview(i) is data on array at index i. lstview(i + 1) is data on array at index i + 1.
        ' Why is i + 1? cause in this looping we use step 2, so if index i is header name then index (i + 1) is header width.
        ' i=0 is header name, i=1 is header width, i=2 is header name, i=3 is header width, and so on.
        ' Call ColumnName(lvwStud, "First Name", 40, "Last Name", 40, "Address", 70)
        ' you can see that "first name' is at index i (start from 0), at index i + 1 is 40 (0 + 1), and so on.
        Next i
    End With

End Sub

Hello sir thank you for putting comments, it really helps me a lot.sir question, is it necessary to make an array in displaying the column header in the listview?

sir can i ask with you where did you get idea on this or what reference book did you use sir?.Please help me thank you in advance hoping for your positive response.

is it necessary to make an array in displaying the column header in the listview?

Not necessary too. I make this code for flexibility purpose. so, you don't have to create new code for new header. just change the parameters on array when you call it. But, if you don't have many list view to set or if your headers is never change then it's not necessary to use array.
You can use this simply following code to add header without array :

With ListView1
    .View = lvwReport ' make listview looks like report view
    .ColumnHeaders.Clear ' Clear header
    .ColumnHeaders.Add , , "First Name", 2000 ' Add header name n width
    .ColumnHeaders.Add , , "Last Name", 2000 ' Add header name n width
    .ColumnHeaders.Add , , "Address", 3000 ' Add header name n width
End With

sir can i ask with you where did you get idea on this or what reference book did you use sir?

i coded this procedure when i was in college and i don't have any reference book.

Not necessary too. I make this code for flexibility purpose. so, you don't have to create new code for new header. just change the parameters on array when you call it. But, if you don't have many list view to set or if your headers is never change then it's not necessary to use array.
You can use this simply following code to add header without array :

With ListView1
    .View = lvwReport ' make listview looks like report view
    .ColumnHeaders.Clear ' Clear header
    .ColumnHeaders.Add , , "First Name", 2000 ' Add header name n width
    .ColumnHeaders.Add , , "Last Name", 2000 ' Add header name n width
    .ColumnHeaders.Add , , "Address", 3000 ' Add header name n width
End With

i coded this procedure when i was in college and i don't have any reference book.

Hello sir, Thank you for the reply and showing another examples it really helps me. Wow,that's nice sir. i hope i can be like you too sir.More power to you sir...Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.