无法将类型为“System.Data.DataRowView”的对象强制转换为“System.String”。 [英] Unable to cast object of type 'System.Data.DataRowView' to type 'System.String'.

查看:286
本文介绍了无法将类型为“System.Data.DataRowView”的对象强制转换为“System.String”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从列表框中读取所有选择项并将其保存到列表中。但它显示错误无法将类型'System.Data.DataRowView'的对象强制转换为'System.String'。



I try to read all the selecteditem from a listbox and save it into a list. But it showing the error "Unable to cast object of type 'System.Data.DataRowView' to type 'System.String'."

List<string> Stringlist = new List<string>() ;

foreach (string selectedItem in ListBox.SelectedItems)
{
      Stringlist .Add(selectedItem);
}





请帮助,thkz



please help, thkz

推荐答案

使用此代码改为获取特定列表框中的所有选定值,例如listbox1:



Use this code instead to fetch all the selected values in a particular listbox, say listbox1 :

List<string> Stringlist = new List<string>();
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            if (ListBox1.Items[i].Selected == true)
            {
                Stringlist.Add(ListBox1.Items[i].ToString());
            }
        }</string></string>





Listbox.selecteditem不是字符串,因此存在转换错误...

使用上面的代码,我想你会得到想要的结果..



谢谢



Listbox.selecteditem is not string so the conversion error was there...
Use the above code and I think you will get the desired result..

Thanks


看看文档 [ ^ ],此集合不是一组 string s。
Look at the documentation[^], this collection is not a set of strings.


查看文档 [<一个href =http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditems.aspxtarget =_ blanktitle =New Window> ^ ],此集合不是一组字符串 s。
Look at the documentation[^], this collection is not a set of strings.


这篇关于无法将类型为“System.Data.DataRowView”的对象强制转换为“System.String”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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