如何将数据表行转换为字符串 [英] How to convert data table row to string

查看:99
本文介绍了如何将数据表行转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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





尝试将数据表行中的值插入到wpf组合框和



我尝试过:



i尝试将datatable中的行转换为字符串,但它在brngname变量中生成了System.data.datarow。如何将数据表中的行转换为字符串?



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?

推荐答案

您不能将行转换为类似的字符串,Row是单元格的集合而不是单个实体。您必须获取字符串中所需的所有单元格的值,并将它们连接起来(使用StringBuilder)。
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).


DataRow的字符串版本 System.Data.DataRow,因为系统不知道DataRow可能包含什么,您可能想要显示哪些部分,或者您希望它们如何格式化。



如果您想要的是将行内容作为连接字符串,则循环遍历Row.Cells集合,并使用StringBuilder将内容转换为单个字符串以连接它们一起。



如果你想更聪明,那么使用DataTable作为ComboBox的DataSource,并将DisplayMember属性设置为仅显示您感兴趣的列:与Comboboxes一步一步的WPF数据绑定 [ ^ ]
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[^]


这篇关于如何将数据表行转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆