Hi,
Currently, I have 3 listboxs' data, but I don't know how to create 3 columns and show them with datagrid control.
Does anyone give me a guid line how to show listbox's data with datagrid?
regards
Hi,
Currently, I have 3 listboxs' data, but I don't know how to create 3 columns and show them with datagrid control.
Does anyone give me a guid line how to show listbox's data with datagrid?
regards
Good question, . In VB6 the DataGrid 6.0 is a data-aware control, so you do not push values into cells directly. Instead, build an ADO Recordset in memory that has three string fields, fill it from your three ListBoxes, then bind the grid to that Recordset. That approach also avoids the 7005 "Rowset not available" error you saw when trying to write to cells before the grid is bound. Key points: use a client-side cursor, append fields while the Recordset is closed, call Open, then use AddNew to insert rows. (learn.microsoft.com)
Here is a reusable helper that fabricates a Recordset from three ListBoxes:
Private Function BuildRsFromLists(l1 As ListBox, l2 As ListBox, l3 As ListBox) As ADODB.Recordset
Dim rs As ADODB.Recordset: Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
With rs.Fields
.Append "Col1", adVarChar, 255
.Append "Col2", adVarChar, 255
.Append "Col3", adVarChar, 255
End With
rs.Open
Dim i As Long, n As Long
n = l1.ListCount: If l2.ListCount < n Then n = l2.ListCount
If l3.ListCount < n Then n = l3.ListCount
For i = 0 To n - 1
rs.AddNew Array("Col1","Col2","Col3"), Array(l1.List(i), l2.List(i), l3.List(i))
Next
Set BuildRsFromLists = rs
End Function How to use it:
n - 1, not ListCount.If the grid shows nothing, confirm the Recordset is open and client-side (adUseClient) before binding. The ADO docs on client-side cursors, appending fields, and opening/adding rows cover the pattern above. (learn.microsoft.com)
Jump to Post— Jx_Man 987Currently, I have 3 listboxs' data, but I don't know how to create 3 columns and show them with datagrid control.
Does anyone give me a guid line how to show listbox's data with datagrid?You can fill recordset with listbox items then you can set datagrid source …
HI,
Check this out:
http://www.codeproject.com/Articles/24180/DataGridView-Control-with-ListBox
Please let me know if you find it useful.
Hi RvSon,
I am using VB6.0. So, your information will not be related with my case directly. Normally, system should have a property to set how many columns we want to use.
But, I can't find it. So, I use default to update "DataGrid". Then, system will prompt error message like
Run time error 7005: RowSet not avaliable
This is my sample code.
DataGrid1.Row = 1
DataGrid1.Columns(2).Text = "12"
Do you know how to make a setting?
regards
Currently, I have 3 listboxs' data, but I don't know how to create 3 columns and show them with datagrid control.
Does anyone give me a guid line how to show listbox's data with datagrid?
You can fill recordset with listbox items then you can set datagrid source with current recordset.
see this example :
Private Sub Command1_Click()
Dim rsTest As New ADODB.Recordset
' create new column in recordset named listbox1, listbox2, listbox3
' every column has unique name
With rsTest.Fields
.Append "Listbox1", adBSTR
.Append "Listbox2", adBSTR
.Append "Listbox3", adBSTR
End With
rsTest.Open
For i = 0 To List1.ListCount
' add every listbox items into each column
With rsTest
.AddNew
.Fields("Listbox1") = List1.List(i)
.Fields("Listbox2") = List2.List(i)
.Fields("Listbox3") = List3.List(i)
.Update
End With
Next i
Set DataGrid1.DataSource = rsTest
End Sub
Private Sub Form_Load()
With List1
For i = 1 To 10
.AddItem i
Next i
End With
With List2
For j = 11 To 20
.AddItem j
Next j
End With
With List3
For k = 21 To 30
.AddItem k
Next k
End With
End Sub

Hi Jx Max,
That is the information what I want. It is working fine.
Thanks.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.