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

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

问题描述

我希望有人可以提供帮助。但每当我跑我的code和尝试查看高分我得到的回到我的列表框是 System.Data.DataRowView

任何人都可以看到,为什么?

code:

 的MySqlConnection的myconn =新的MySqlConnection(connStr);字符串sqlStr =SELECT CONCAT(名称,'',分数)为NameAndScore+
                从高分ORDER BY分数DESC;MySqlDataAdapter的dAdapter =新MySqlDataAdapter的(sqlStr,的myconn);
数据表dTable =新的DataTable();
dAdapter.Fill(dTable);
dAdapter.Dispose();
lstNames.DisplayMember =NameAndScore;
lstNames.DataSource = dTable;


解决方案

我总是要处理这个问题,即使我设置的DisplayMember和ListBox的ValueMembers。
您当前的code是正确的,应该工作,如果你需要访问的任何列的当前选择的项值你的 dTable 你可以让他们这样做的:

  DataRowView的DRV =(DataRowView的)lstNames.SelectedItem;
字符串valueOfItem = DRV [NameAndScore]的ToString()。

我喜欢让整个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.

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;

解决方案

I always have to deal with this problem, even if I set the DisplayMember and ValueMembers of the ListBox. 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();

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天全站免登陆