Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load Listbox Data into Datagrid?

Status
Not open for further replies.

ilovevbsomuchimmad

Programmer
Feb 5, 2007
18
0
0
GB
Hi i have the following coding which adds text from textboxes in to a datagrid, with these text boxes there is a listbox, is it possible to add all the data within the listbox to a cell or a few cells in the datagrid? Thanks

Addorder("Name") = TextBox1.Text
Addorder("Dept") = TextBox2.Text
 
with vb2005 and the DataGridView object you could do the following:

Try

Dim ds As New DataSet
Dim I As Integer = 0
Dim dt As New DataTable("Table1")
Dim dc As New DataColumn("column1", System.Type.GetType("System.String"))
dt.Columns.Add(dc)
For I = 0 To Me.ListBox1.Items.Count - 1
Dim row As DataRow
row = dt.NewRow()
row("column1") = Me.ListBox1.Items.Item(I)
dt.Rows.Add(row)
Next
ds.Tables.Add(dt)
Me.DataGridView1.DataSource = ds.Tables(0)

Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
 
Hi, I used your coding but had to change the Datagridview to Datagrid1 as that is what its called. The problem I have now is Only the "listbox" Column is shown, however I want all of the columns to be shown.
 
My example just shows how you could add listbox values to a dataset and then populate a datagridview with the datasets rows.

You should try to use your listbox values and add them to your dataset row/rows in the relevant places or add a column to the dataset and fill the cells with the relevant value of your choice, where you decide. My example creates a new dataset just to demonstrate the scenario :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top