Hi all,

Is it possible to select multiple records (rows) in "DataGrid" and then delete all selected items? Because I can't find any properties to enable for multiple selection.
Whenever I try to delete a record, system always prompt the run time error.

    Private Sub Command4_Click()
        Dim count As Integer
        Dim index As Integer

        count = DataGrid1.SelBookmarks.count

        If (count > 0) Then

            index = DataGrid1.Row

            Call DataGrid1.SelBookmarks.Remove(index)
        Else
            MsgBox ("You must select a row")
        End If
    End Sub

I didn't use database in my application.
Does any one know where is my problem?

regards,

U can use Datarow Event of GridView

Hi hirenpatel,

I don't know how to use "Datarow Event" in vb6.0. I also can't find any same program in other website.
Can you show me how to use it for "Multiple Select" and "Delete Multiple Records"?

regards

Is it possible to select multiple records (rows) in "DataGrid"

Because I can't find any properties to enable for multiple selection.

Yes.. Just press ctrl while you selecting items.

delete all selected items?

I not sure you can do this. Your code just remove bookmark not remove the items.
Datagrid currently using for displaying data from database.

I didn't use database in my application.

Why not use listview instead of datagrid?

Hi Jx Max,

Thanks for your suggestion. I think I have choosen the wrong control.
Actually, I want to display a lot of data read out from text file. I will let user to delete and edit some fields or records. I also want to add one feature in my GUI. After finishing the editing, I want to change backgroud colour of each record. That's why I try to use "dataGird".

Is it possible all requirements can be done on "ListView"? Which control is best for me?

regards.

This Following code is using listview to make background colour and delete multiple items.

Private Sub SetListview()
' Set listview
With ListView1
    .View = lvwReport
    .FullRowSelect = True
    .ColumnHeaders.Add , , "Id", 1100
    .ColumnHeaders.Add , , "First Name", 1400
    .ColumnHeaders.Add , , "Last Name", 1400
    .ColumnHeaders.Add , , "Email", 1700
End With

' Insert Data
With ListView1.ListItems
    .Add , , "1"
    .Item(1).SubItems(1) = "Andre"
    .Item(1).SubItems(2) = "White"
    .Item(1).SubItems(3) = "Andre@White.com"

    .Add , , "2"
    .Item(2).SubItems(1) = "Danny"
    .Item(2).SubItems(2) = "Burnett"
    .Item(2).SubItems(3) = "Danny@Burnett.com"

    .Add , , "3"
    .Item(3).SubItems(1) = "Edward"
    .Item(3).SubItems(2) = "Carter"
    .Item(3).SubItems(3) = "Edward@Carter.com"

    .Add , , "4"
    .Item(4).SubItems(1) = "Anne"
    .Item(4).SubItems(2) = "Witter"
    .Item(4).SubItems(3) = "Anne@Witter.com"

    .Add , , "5"
    .Item(5).SubItems(1) = "Vicky"
    .Item(5).SubItems(2) = "Myron"
    .Item(5).SubItems(3) = "Vicky@Myron.com"
End With
End Sub

Private Sub MakeItColorful(lstview As ListView)
If lstview.ListItems.Count > 0 Then

    picBG.Width = lstview.Width
    picBG.Height = lstview.ListItems(1).Height * (lstview.ListItems.Count)
    picBG.ScaleHeight = lstview.ListItems.Count
    picBG.ScaleWidth = 1
    picBG.DrawWidth = 1
    picBG.Cls
    For i = 1 To lstview.ListItems.Count
        If (lstview.ListItems(i) Mod 2) = 0 Then
            picBG.Line (0, i - 1)-(1, i), &H80FFFF, BF
        Else
            picBG.Line (0, i - 1)-(1, i), &HC0FFC0, BF
        End If
    Next i

    lstview.Picture = picBG.Image
Else
    picBG.Cls
    lstview.Picture = picBG.Image
End If
End Sub

Private Sub Command1_Click()
    With ListView1
        For i = .ListItems.Count To 1 Step -1
            If .ListItems(i).Selected Then
                .ListItems.Remove (.ListItems(i).Index)
            End If
        Next
    End With

End Sub

Private Sub Form_Load()
With ListView1
    .GridLines = True
    .View = lvwReport
End With

SetListview
MakeItColorful ListView1

End Sub

825db81dd0b779fc92e89e8d34ec3404

Hi Jx Man,

Your program is very helpful for me.
I would like to ask one question. Is it possibe to add "Vertical scrollbar" in ListView because data read from file is very huge?
It will have about over 1000lines. So, I want to make for easy viewing.

Do you have any suggestion?

regards

Is it possibe to add "Vertical scrollbar" in ListView because data read from file is very huge?

It will added automatically (vertical scrollbar or horizontal scrollbar) when data is more longer than listview.

Hi Jx Max,

My one is not automatically yest. Please take a look attached file.

Thanks

Which version are you using?

Hi Jx Man,

I am using VB6.0. I know where is my problem. I set "FlatScrollBar = True" in properties. That's why I can't see the scrollbar.
May I ask you about UI. How do you change the color of "Main Form's Border"? I am quite interesting how you do that.

regards

How do you change the color of "Main Form's Border"?

I'm not change it. I used win7 and that is win7 themes.

Hi Jx Man,

Thanks.
According to your previous method, itself is working fine. But, "ID" and "background color" are difficult to maintain (match) after deleting all selected item.
So, I use "fore color". It will not need to control anything while deleting.

Please take a look the attached file

regards,

But, "ID" and "background color" are difficult to maintain (match) after deleting all selected item.

Don't use ID since ID didn't related to any data and the code will remove all data in selected row.

For background color don't mind it so much. when you load over 1000 lines of text file it will completely cover the white rows.

Hi Jx Man,

I change "ID" as "No" in my project. So, user can be easily know how many rows are there.
I also add "Edit" button to update one of the fields.When user finish the editing, I will change the background color for related rows.
One thing I have to be careful, I still need to maintain previous "Background Color". When user see background color changed for all lists, they will realise that "Editing" process is finished.
You know I need to update row "No" and "Background Color" Whenever user deletes any row. That's why "ID" and "Background Color" are quite important for me.

regards

I change "ID" as "No" in my project. So, user can be easily know how many rows are there.

Why you don't count listview items to show it? it's more easy than user have to drag over the 1000 lines of records.

You know I need to update row "No" and "Background Color" Whenever user deletes any row

No one of control (datagrid,listview,flexgrid,etc) can't handling "No" update. When you remove 1 record, it will completely remove entire data in selected row.

I also add "Edit" button to update one of the fields.When user finish the editing, I will change the background color for related rows.

You can manipulate forecolor and bold to mark edited rows.

There is new codes for what you wants :
You can delete any rows without worrying the number of records.
After editing selected row will change forecolor to Red and set as Bold

Private Sub SetListview()
' Set listview
With ListView1
    .View = lvwReport
    .FullRowSelect = True
    .ColumnHeaders.Add , , "Id", 1100
    .ColumnHeaders.Add , , "First Name", 1400
    .ColumnHeaders.Add , , "Last Name", 1400
    .ColumnHeaders.Add , , "Email", 1700
End With

' Insert Data
With ListView1.ListItems
    .Add , , "1"
    .Item(1).SubItems(1) = "Andre"
    .Item(1).SubItems(2) = "White"
    .Item(1).SubItems(3) = "Andre@White.com"

    .Add , , "2"
    .Item(2).SubItems(1) = "Danny"
    .Item(2).SubItems(2) = "Burnett"
    .Item(2).SubItems(3) = "Danny@Burnett.com"

    .Add , , "3"
    .Item(3).SubItems(1) = "Edward"
    .Item(3).SubItems(2) = "Carter"
    .Item(3).SubItems(3) = "Edward@Carter.com"

    .Add , , "4"
    .Item(4).SubItems(1) = "Anne"
    .Item(4).SubItems(2) = "Witter"
    .Item(4).SubItems(3) = "Anne@Witter.com"

    .Add , , "5"
    .Item(5).SubItems(1) = "Vicky"
    .Item(5).SubItems(2) = "Myron"
    .Item(5).SubItems(3) = "Vicky@Myron.com"
End With
End Sub

Private Sub MakeItColorful(lstview As ListView)
If lstview.ListItems.Count > 0 Then

    picBG.Width = lstview.Width
    picBG.Height = lstview.ListItems(1).Height * (lstview.ListItems.Count)
    picBG.ScaleHeight = lstview.ListItems.Count
    picBG.ScaleWidth = 1
    picBG.DrawWidth = 1
    picBG.Cls
    For i = 1 To lstview.ListItems.Count
        If (lstview.ListItems(i) Mod 2) = 0 Then
            picBG.Line (0, i - 1)-(1, i), &H80FFFF, BF
        Else
            picBG.Line (0, i - 1)-(1, i), &HC0FFC0, BF
        End If
    Next i

    lstview.Picture = picBG.Image
Else
    picBG.Cls
    lstview.Picture = picBG.Image
End If
End Sub

Private Sub Command1_Click()
    With ListView1
        For i = .ListItems.Count To 1 Step -1
            If .ListItems(i).Selected Then
                .ListItems.Remove (.ListItems(i).index)
            End If
        Next
    End With

NumOfListView ListView1
MakeItColorful ListView1

End Sub

Private Sub NumOfListView(lst As ListView)
Dim a, b As Variant
Dim temp() As String

a = lst.ListItems.Count
b = lst.ColumnHeaders.Count - 1

    With lst
    ReDim temp(a, b)

        For i = 0 To .ListItems.Count - 1
            temp(i, 0) = i + 1
            For j = 1 To .ColumnHeaders.Count - 1
                temp(i, j) = .ListItems.Item(i + 1).SubItems(j)
            Next
        Next

    .ListItems.Clear

        For x = 1 To UBound(temp)
            With .ListItems
                .Add , , temp(x - 1, 0)
                For y = 0 To lst.ColumnHeaders.Count - 2
                    .Item(x).SubItems(y + 1) = temp(x - 1, y + 1)
                Next
            End With
        Next
    End With
End Sub
Public Sub ColorListviewRow(lv As ListView, RowNbr As Integer, RowColor As OLE_COLOR)
    Dim itmX As ListItem
    Dim lvSI As ListSubItem
    Dim intIndex As Integer

    Set itmX = lv.ListItems(RowNbr)
    itmX.ForeColor = RowColor
    For intIndex = 1 To lv.ColumnHeaders.Count - 1
        Set lvSI = itmX.ListSubItems(intIndex)
        lvSI.ForeColor = RowColor
        lvSI.Bold = True
    Next

    lv.ListItems(RowNbr).Selected = True

    Set itmX = Nothing
    Set lvSI = Nothing

End Sub

Private Sub Command2_Click()
With ListView1
    index = .SelectedItem.index
    .ListItems(index).Text = Text1.Text
    .ListItems(index).SubItems(1) = Text2.Text
    .ListItems(index).SubItems(2) = Text3.Text
    .ListItems(index).SubItems(3) = Text4.Text
End With

ColorListviewRow ListView1, CInt(index), vbRed
End Sub

Private Sub Form_Load()
With ListView1
    .GridLines = True
    .View = lvwReport
End With

SetListview
MakeItColorful ListView1
End Sub

Private Sub ListView1_Click()
    Dim index As Integer
    index = ListView1.SelectedItem.index

    Text1.Text = ListView1.ListItems(index).Text
    Text2.Text = ListView1.ListItems(index).SubItems(1)
    Text3.Text = ListView1.ListItems(index).SubItems(2)
    Text4.Text = ListView1.ListItems(index).SubItems(3)
End Sub

88c91e840f5a8bdcc61d2534d9d82f82

commented: This is awesome dude..wan't to try it.. +2

Hi Jx Man,

Thanks for you help. It is very useful for me.

regards

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.