Add Data from Listbox to dataTable

vks.gautam1

Well-known member
Joined
Oct 10, 2008
Messages
78
Location
Chandigarh, India
Programming Experience
Beginner
i want to send data from list box to datagridview. so first i tried to fill dataTable.then add that to dataset. then add data to datagridview.
Is this right way

It is giving error=column required valid data type
VB.NET:
Dim ds As New DataSet
dim i as integer
            Dim table As New DataTable("list")
            
            Dim dat As DataColumn = New DataColumn("Dat")
            dat.DataType = System.Type.GetType("System.string")
            table.Columns.Add(dat)
            Dim dr1 As DataRow
            For i = 0 To ListBox1.Items.Count - 1
                dr1 = table.NewRow
                For j As Integer = 0 To ListBox1.Items.Count - 1
                    dr1(j) = ListBox1.Items(j).ToString

                Next
                table.Rows.Add(dr1)
            Next
            ds.Tables.Add(table)
            DataGridView1.DataSource = ds.Tables(0)

there is problem in adding data from listbox to data row.
 
If there are 10 items in your listbox, you want a datatable of 10 rows by 10 columns, giving 100 cells.. COrrect?
 
I only one column in list box
This my new code & it worked.
VB.NET:
 Dim i As Integer
            Dim ds As New DataSet
            Dim table As New DataTable("list")
            With table.Columns
                .Add("Data", GetType(String))
            End With

            With table.Rows
                For i = 0 To ListBox1.Items.Count - 1
                    .Add(ListBox1.Items(i))
                Next
            End With

            ds.Tables.Add(table)
            DataGridView1.DataSource = ds.Tables(0)
 
Back
Top