Click here to Skip to main content
15,910,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre> Dim a As New ClsKoneksi
        Dim query As String
        query = "SELECT KodeBarang FROM Barang"
        dt = a.executequerydata(query)
        Dim names As New List(Of String)
            For i = 0 To dt.Rows.Count() - 1
                Dim brngname As String = Convert.ToString(dt.Rows(i))
                names.Add(brngname)
            Next
            For i = 0 To names.Count() - 1
                BARANGLIST.Items.Add(names(i).ToString)
            Next


try to insert value from data table row to wpf combobox and

What I have tried:

i try to convert row in datatable to string but it produced "System.data.datarow" in brngname variable. how can i convert row in data table to string?
Posted
Updated 5-Oct-18 2:44am
Comments
MadMyche 5-Oct-18 10:13am    
Your SQL query is only returning the KodeBarang column from the database table. Did you want other columns returned; such as HargaBarang?

You cannot convert a row into a string like that, a Row is a collection of cells not a single entity. You must take the values of all the cells you want in the string, and concatenate them (use a StringBuilder).
 
Share this answer
 
Comments
MadMyche 5-Oct-18 10:07am    
I'm not a VB person, but the SQL query looks like he is only returning one column from the table; there wouldn't be anything to concatenate at the row level.
Richard MacCutchan 5-Oct-18 12:36pm    
It doesn't matter, a row is still a list, albeit with only one item. So he still needs to collect the cell entries one at a time. Saying row(i).ToString() will only ever give you the class of a Datatable row.
The string version of a DataRow is "System.Data.DataRow", because the system has no idea what the DataRow may contain, what part(s) of it you might want to display, or how you want them formatted.

If all you want is the row content as a concatenated string, then loop through the Row.Cells collection, and convert the content to individual strings, using a StringBuilder to "join" them together.

If you want to be cleverer, then use the DataTable as a DataSource for the ComboBox, and set the DisplayMember property to show only the column you are interested in: Step by Step WPF Data Binding with Comboboxes[^]
 
Share this answer
 
Comments
MadMyche 5-Oct-18 10:10am    
The system may not know what the row contains, but from the query being run it looks like there is only one column per row.

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