Edit or Delete Selected Item from MSHFlexGrid in VB6.0 Tips

In Visual Basic, If you are using MSHFlexGrid in your Application for Listing items then sometime you need to Edit or Delete record or items at run time. So, here we learn  Edit Selected Items from MSHFlexGrid and Delete Selected Items in MSHFlexGrid in VB6.0. For showing data in table list, there are many controls in VB6.0 to use such as: Listview, Gridview, FlexGrid, MSHFlexGrid.

This is the post where we learn about editing or deleting data in MSHFlexGrid.

Attach MSHFlexGrid Component to Toolbox.

It is necessary to consider that at least one record can be exist on this control for edit or delete item. Add record at run time or show record from database.

Change MSHFlexGrid Name with RecMSGrid containing some record.
Items in MSHFlexGrid for edit or delete
Items details

Now, drag some controls from Toolbox to Form1. There is following controls we drag and set its Name and Caption as bellow. Drag Textbox, Labels, Command buttons for further Process.

TextBox Names:
Define TextBox Name as 'CSrno_Txt', 'CProduct_Txt', 'CQuantity_Txt', 'CAmount_Txt'.

Labels Names:
Define Labels Caption as 'C_Srno:', 'Product:', 'Quantity:', 'Amount:', 'Selected Row:'.
Drag another Label and set its Name as ‘Row_Lbl’.

CommandButtons Names:
Define buttons Caption as 'Edit selected Item', 'Delete selected Item' and Name as 'Edit_Cmd', 'Delete_Cmd'.

Control’s adjustment to Form1 as describe in following image.

Control's description for edit or delete item
Control's description

(1). Edit items From MSHFlexGrid:

First of all, select particular item from RecMSGrid, when you select item, the item selected index will show in ‘Row_Lbl’ label and particular selected item will show in related TextBox. For this activity, write code in RecMSGrid's ‘RowColChange()’ events.

Add column header and data in MSHFlexgrid.

Get MSHFlexGrid Selected Row Value in TextBox Code:
Private Sub RecMSGrid_RowColChange()

   With RecMSGrid
      'Put selected index in (Row_Lbl) Label 
       Row_Lbl.Caption = .Row
      'put every cell value in textbox of selected items
      CSrno_Txt.Text = .TextMatrix(RecMSGrid.Row, 0)
      CProduct_Txt.Text = .TextMatrix(RecMSGrid.Row, 1)
      CQuantity_Txt.Text = .TextMatrix(RecMSGrid.Row, 2)
      CAmount_Txt.Text = .TextMatrix(RecMSGrid.Row, 3)
   End With

End Sub

Now, for edit item from this control write code on ‘Edit_Cmd’ command button’s ‘Click()’ events as shown bellow.

Set Text Value in Particular Row Code:

Private Sub Edit_Cmd_Click()

   With RecMSGrid
      'assign index value in integer variable
      Dim rw As Integer
      rw = Val(Row_Lbl.Caption)
     .TextMatrix(rw, 0) = CSrno_Txt.Text
     .TextMatrix(rw, 1) = CProduct_Txt.Text
     .TextMatrix(rw, 2) = CQuantity_Txt.Text
     .TextMatrix(rw, 3) = CAmount_Txt.Text
   End With

   MsgBox "Record Edited Successgully.", vbInformation

End Sub

When you apply this code, the output will view such as bellow picture.
Edit selected item output from MSHFlexGrid
Edit items output

In this way we can edit items from RecMSGrid's.

(2). Delete Items from MSHFlexGrid :

For Delete Selected Items from MSHFlexGrid as RecMSGrid, just write code in command button’s ‘Click()’ event or directly press ‘Delete’ button from keyboard. For delete items, write code on ‘KeyDown(KeyCode As Integer, Shift As Integer)’ event of RecMSGrid.

Settings Column width, Color, Font and Grid style of MSHFlexGrid

Press ‘Delete’ button from keyboard.

Private Sub RecMSGrid_KeyDown(KeyCode As Integer, Shift As Integer)

    'KeyCode = 46 define the ‘Delete’ Button of keyboard
    If KeyCode = 46 Then
        'write execution code here
    End if

End Sub

Now, for delete data from this control write code on ‘Delete_Cmd’ command button’s ‘Click()’ event as shown bellow.

Delete Selected Row Code:
Private Sub Delete_Cmd_Click()

    Dim i As Integer
    With RecMSGrid
        'Check if selected record not equal=1
        If  .RowSel <> 1 Then
             'apply loop from selected record to till last record
             For i = Val(Row_Lbl.Caption) To  .Rows - 2
                     'insert next item in previous item
                     .TextMatrix(i, 0) = .TextMatrix(i + 1, 0)
                     .TextMatrix(i, 1) = .TextMatrix(i + 1, 1)
                     .TextMatrix(i, 2) = .TextMatrix(i + 1, 2)
                     .TextMatrix(i, 3) = .TextMatrix(i + 1, 3)
             Next i
            'Decrease every row on every loop
            .Rows = .Rows - 1
            'If we have only one record, apply cleaning of every cell or first record
            .TextMatrix(RecMSGrid.Row, 0) = ""
            .TextMatrix(RecMSGrid.Row, 1) = ""
            .TextMatrix(RecMSGrid.Row, 2) = ""
            .TextMatrix(RecMSGrid.Row, 3) = ""
      End If
   End With

   MsgBox "Record Deleted Successfully.", vbInformation

End Sub

This code will show delete process through following image.

Delete selected item output from MSHFlexGrid
Delete items output

In this way you can Delete Selected item from MSHFlexGrid.

Show Database Record with ADODC in MSHFlexGrid.

Items Edit or delete from MSHFlexGrid in vb6.0 easily Describe by above given steps, you can apply all these steps for better work in coding.