为什么我得到“System.Data.DataRowView"?而不是我的列表框中的真实值? [英] Why I get "System.Data.DataRowView" instead of real values in my Listbox?

查看:35
本文介绍了为什么我得到“System.Data.DataRowView"?而不是我的列表框中的真实值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以提供帮助.但是每当我运行我的代码并尝试查看 highscore 时,我返回的列表框都是 System.Data.DataRowView.

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.

有人能明白为什么吗?

代码:

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;

推荐答案

我总是要处理这个问题,即使我设置了DisplayMemberValueMembers列表框.

I always have to deal with this problem, even if I set the DisplayMember and ValueMembers of the List Box.

您当前的代码是正确的并且应该可以工作,如果您需要访问 dTable 的任何列的当前选定项目值,您可以让他们这样做:

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:

DataRowView drv = (DataRowView)lstNames.SelectedItem;
String valueOfItem = drv["NameAndScore"].ToString();

我喜欢获取整个 DataRowView 的一点是,如果您有更多的列,您仍然可以访问它们的值并使用它们执行任何您需要的操作.

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.

这篇关于为什么我得到“System.Data.DataRowView"?而不是我的列表框中的真实值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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