Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am studying about VB 6.
I have a datagrid in my application and I want to create header for this datagrid and then add data from a array to this datagrid.

Can anybody help me please!
thanks
:doh:
Posted
Updated 15-Dec-10 22:11pm
v2
Comments
Dalek Dave 16-Dec-10 4:11am    
Edited for Grammar and Spelling.

If your DataGrid is designed to have a single column bound to Field001.
Or you just add the fields in the code below, and populate as per your need.

VB
Sub addArrayToGrid(grid As DataGrid, arr() As String)
    Dim rs As ADODB.Recordset
    'Make a recordset
    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    'add your fields
    rs.Fields.Append "Field001", adVarChar, 100
    'Open recordset with no database
    rs.Open , Nothing
    'add your records manually (you do this from array)
    For i = 0 To UBound(arr)
        rs.AddNew
        rs.Fields("Field001").Value = arr(i)
    Next
    'bind to grid
    Set grid.DataSource = rs
End Sub
 
Share this answer
 
Comments
ngthtra 16-Dec-10 23:06pm    
thanks prerakpatel very much. your code is very good but If my datagrid have o lot of fields and read data form a file which have may line, and data of each line I will add to datagrid.
so how would this code edited ?

thaks about you help!
ngthtra 16-Dec-10 23:58pm    
when I used your code, there are error happen, application show error line"Dim rs As ADODB.Recordset" with message "user-defined type not defined".So how to call your code to perform and fix bug?
Prerak Patel 17-Dec-10 2:15am    
You need to add reference to ADODB
ngthtra 17-Dec-10 3:57am    
with datagrid is there any property to set aglign center for the fields?
Prerak Patel 17-Dec-10 5:39am    
If your problem is solved, mark it as answer.
BTW, datagrid1.Columns(0).Alignment=dbgCenter
The main problem you are having is in the opening line of your question.

Do not study VB6, nobody supports it.
It is an unloved child and it will not lead to a distinguished career in development.

Put down the dinosaur books and learn VB.Net.

Seriously!
 
Share this answer
 
Comments
ngthtra 16-Dec-10 5:59am    
thanks about your advice but my teacher ask me write a application in VB 6.
I am new programer.
I hope that you will help me!
thaks very much
thanks prerakpatel very much. your code is very good but If my datagrid have o lot of fields and read data form a file which have may line, and data of each line I will add to datagrid.(add for row)
so how would this code edited ?

thaks about you help!
 
Share this answer
 
v2
Comments
Prerak Patel 17-Dec-10 2:23am    
Don't post comment as an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900