Add Column Header and Add Items in MSHFlexgrid in vb6.0 Tips

Adding Column Header in MSHFlexGrid and add items with its related columns, it’s an easy process, but one thing, in MSHFlexGrid you have to first define Number for Columns then you can add Column Header according to Defined Numbers. You have to consider about that you cannot define more header name than number of Columns.

This post also describe the way to Add Items in MSHFlexGrid in VB6.0 by SKOTechLearn.

First, Add MSHFGrid Component to Toolbox. After that go to Properties and set its column number.

Set Column and Row Number in MSHFlexGrid:

Suppose, if we set columns and rows like bellow, the further steps will describe the Setting of column header.

Columns=4
Rows=1
Fixed Rows=1
Fixed Cols=0

Note: Fixed Rows is use for set number of Header.

After apply rows and columns setting as mention in above image, write code in Form’s Load event.
Suppose we set MSHFlexGrid Name as "MSFG1".

Set Column Header in MSHFlexGrid :

For this process we will use code process. There is no facilities like Listview to add Header Name through Properties window.

Code:
Private Sub Form_Load()
  With MSFG1
    .Clear
    .Rows = 2
    .TextMatrix(0, 0) = "Sr.no."
    .TextMatrix(0, 1) = "Worker_Name"
    .TextMatrix(0, 2) = "Worker_Age"
    .TextMatrix(0, 3) = "Worker_Mo.No."
  End With
End Sub

Description:
.Clear Clear rows and columns.
.Rows: Set row number for record insert.
.TextMatrix(Row, Column): In TextMatrix, assign row number and column number in which you can set value or string.

Note: In every record insertion, it is necessary to increase 'MSFG1.Rows'.

Add Record or Add Items in MSHFlexGrid :

Now, drag button to Form and set Caption as 'Add Record' and set Name as 'Add_Cmd'. After that, double click on this command button and type code on its click event as shown bellow.

Changing width, Color, Font and Grid style in MSHFGrid

Code:
Private Sub Add_Cmd_Click()
  With MSFG1
      .Rows = 3
      .TextMatrix(1, 0) = 1
      .TextMatrix(1, 1) = "ABCD"
      .TextMatrix(1, 2) = 21
      .TextMatrix(1, 3) = "000000"

      .Rows = 4
      .TextMatrix(2, 0) = 2
      .TextMatrix(2, 1) = "CDEF"
      .TextMatrix(2, 2) = 26
      .TextMatrix(2, 3) = "1111111"
    'Like this you can add more items.
  End With
  MsgBox "Records or items added successfully.", vbInformation
End Sub
Runtime and code for add data in MSHFlexGrid
Runtime and code description
Now, we will learn how to show Textbox data in MSHFlexGrid?

Add TextBox Data in MSHFlexGrid :

Suppose, we design Textbox and Labels according to number of header.

Set TextBox Name as:
 Textbox  Name 
Text1 W_Name_Txt
Text2 W_Age_Txt
Text3 W_Mono_Txt
Set Label Caption as:
LabelsCaption
Label1Name of Worker:
Label2Age of Worker:
Label3Worker Mo.No.:
Label4Rows:


Now, write code on Form’s load event as shown bellow.
Private Sub Form_Load()                            
  With MSFG1
     .Clear
     .Rows = 2                            
     .TextMatrix(0, 0) = "Sr.no."                            
     .TextMatrix(0, 1) = "Worker_Name"                            
     .TextMatrix(0, 2) = "Worker_Age"                            
     .TextMatrix(0, 3) = "Worker_Mo.No."                            
     'Rows count show in Row_Lbl                            
     Row_Lbl.Caption = .Rows
  End With
End Sub

This code will show header and row count at runtime.

Now, write code on 'Add_Cmd' button’s click event as shown bellow.
Private Sub Add_Cmd_Click()                            
  With MSFG1
     Row_Lbl.Caption = .Rows
     .Rows = Val(Row_Lbl.Caption) + 1
     .TextMatrix(Val(Row_Lbl.Caption) - 1, 0) = Val(Row_Lbl.Caption) - 1
     .TextMatrix(Val(Row_Lbl.Caption) - 1, 1) = W_Name_Txt.Text
     .TextMatrix(Val(Row_Lbl.Caption) - 1, 2) = W_Age_Txt.Text
     .TextMatrix(Val(Row_Lbl.Caption) - 1, 3) = W_Mono_Txt.Text
  End With
  MsgBox "Records or items saved successfully.", vbInformation
End Sub

After typing this code, run form. Then input value in Worker_Name, Worker_Age and Worker_Mono Texbox and press on 'Add_Cmd' button.

When you pressing on this button, a message box will appear saying 'Records or items saved successfully' as shown bellow. Now, This will show that, the data will add in FlexGrid control.

ADODC connection and show database record in MSHFlexGrid.

MSHFlexGrid Output:

You can understand through given following output view.
Add data in MSHFlexGrid Output
Records Output

You can see the output of record process from Textbox to this component. If you are beginner and want to learn about this component in Visual Basic 6.0, then follow the steps just like I mentioned above. And as it is, I have coded it step by step.

Learning process completed to Add column header in MSHFlexGrid and Add Items in MSHFlexgrid in vb6.0 at runtime.

How to edit or delete items from Listview in vb6.0.

1 comment: