C# 为什么我的列表框中会出现“System.Data.DataRowView”而不是实际值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15428542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why I get "System.Data.DataRowView" instead of real values in my Listbox?
提问by Cain Neal
I'm hoping someone can help. But whenever I run my code and try to view a highscore
all I get back in my listbox is System.Data.DataRowView
.
我希望有人可以提供帮助。但是每当我运行我的代码并尝试查看highscore
我在列表框中返回的所有内容时,都是System.Data.DataRowView
.
Can anyone see why?
谁能看出为什么?
Code:
代码:
MySqlConnection myConn = new MySqlConnection(connStr);
string sqlStr = "SELECT CONCAT(Name, ' ', Score) as NameAndScore " +
"FROM highscore ORDER BY Score DESC";
MySqlDataAdapter dAdapter = new MySqlDataAdapter(sqlStr, myConn);
DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
dAdapter.Dispose();
lstNames.DisplayMember = "NameAndScore";
lstNames.DataSource = dTable;
采纳答案by echavez
I always have to deal with this problem, even if I set the DisplayMember
and ValueMembers
of the List Box.
我总是不得不处理这个问题,即使我设置了列表框的DisplayMember
和ValueMembers
。
Your current code is correct and should work, if you need access to the current selected item value of any column of your dTable
you can get them doing this:
您当前的代码是正确的并且应该可以工作,如果您需要访问您的任何列的当前选定项目值,dTable
您可以让他们这样做:
DataRowView drv = (DataRowView)lstNames.SelectedItem;
String valueOfItem = drv["NameAndScore"].ToString();
What I like about getting the entire DataRowView
is that if you have more columns you can still access their values and do whatever you need with them.
我喜欢获取全部内容的DataRowView
一点是,如果您有更多列,您仍然可以访问它们的值并使用它们执行任何您需要的操作。
回答by Soner G?nül
Set your lstNames.DisplayMember
and lstNames.ValueMember
fields.
设置您的lstNames.DisplayMember
和lstNames.ValueMember
字段。
Gets or sets the property to display for this ListControl.
获取或设置要为此 ListControl 显示的属性。
Gets or sets the path of the property to use as the actual value for the items in the ListControl.
获取或设置要用作 ListControl 中项目的实际值的属性的路径。
This should solve your problem..
这应该可以解决您的问题..
回答by DarK
Like I said in the comments, please add lstNames.DataBind() to your code.
就像我在评论中所说的那样,请将 lstNames.DataBind() 添加到您的代码中。
MySqlConnection myConn = new MySqlConnection(connStr);
string sqlStr = "SELECT CONCAT(Name, ' ', Score) as NameAndScore " +
"FROM highscore ORDER BY Score DESC";
MySqlDataAdapter dAdapter = new MySqlDataAdapter(sqlStr, myConn);
DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
dAdapter.Dispose();
lstNames.DisplayMember = "NameAndScore";
lstNames.ValueMember = "NameAndScore";
lstNames.DataSource = dTable;
EDIT:
编辑:
Can you try this instead:
你可以试试这个吗:
MySqlConnection myConn = new MySqlConnection(connStr);
string sqlStr = "SELECT CONCAT(Name, ' ', Score) as NameAndScore " +
"FROM highscore ORDER BY Score DESC";
myConn .Open();
SqlCommand cmd = new SqlCommand(sqlStr, SQLConnection1);
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
lstNames.Items.Add(rd[0]);
}
rd.Close();
rd.Dispose();
myConn.Close();
回答by Thorsten Dittmar
The following code should work:
以下代码应该可以工作:
DataSet dSet = new DataSet();
dAdapter.Fill(dSet);
lstNames.DisplayMember = "NameAndScore";
lstNames.ValueMember = "NameAndScore";
lstNames.DataSource = dSet.Tables[0];
If it does not work, please update your question and provide us with some information about the columns and values that are actually returned in dSet.Tables[0]
.
如果它不起作用,请更新您的问题并向我们提供一些有关实际返回的列和值的信息dSet.Tables[0]
。
回答by Emad
If you want your list box to show values from an specific column of the data source use lstNames.DataTextField = "SpecialColumnName";
如果您希望列表框显示数据源特定列中的值,请使用 lstNames.DataTextField = "SpecialColumnName";
回答by SijeDeHaan
I use :
我用 :
foreach (DataGridViewRow row in _dataGridView.SelectedRows)
{
var rowObject = row.DataBoundItem as DataRowView;
var array = rowObject.Row.ItemArray;
}
回答by Mohammed Shalabi
just make sure you are typing the name of field same as in datasource in other word it is a case sensitive
只需确保您输入的字段名称与数据源中的名称相同,换句话说,它区分大小写
so : Me.GridLookUpEdit1.Properties.DisplayMember = "cur_code" is different than Me.GridLookUpEdit1.Properties.DisplayMember = "CUR_code"
所以: Me.GridLookUpEdit1.Properties.DisplayMember = " cur_code" 与 Me.GridLookUpEdit1.Properties.DisplayMember = " CUR_code" 不同
回答by user9039555
if you copied the object instead of placing as a new object it will do this. Try deleting the object and putting it back on. I had the same issue and this is what I did. Don't know why it worked, but it did.
如果您复制对象而不是作为新对象放置,它将执行此操作。尝试删除该对象并将其放回原处。我有同样的问题,这就是我所做的。不知道为什么它有效,但它确实有效。
回答by Tamás Solymosi
Only the order of the calls is wrong. First set the DataSource
and only after that set the display member:
只有调用顺序是错误的。首先设置,DataSource
然后才设置显示成员:
lstNames.DataSource = dTable;
lstNames.DisplayMember = "NameAndScore";
回答by roadrunner66
I had the similar problem of picking a single value from a populated Listbox and getting System.Data.DataRowView
.
I was able to solve it by casting the selected item to System.Data.DataRowView
and then
accessing the Row and ItemArray subclasses:
我遇到了类似的问题,即从填充的列表框中选择一个值并获取System.Data.DataRowView
. 我能够通过将所选项目转换为System.Data.DataRowView
然后访问 Row 和 ItemArray 子类来解决它:
var si = ListBox1.SelectedItem;
string val = ((System.Data.DataRowView)si).Row.ItemArray[0].ToString();