绑定列表框与多个字段 [英] Bind ListBox with multiple fields

查看:111
本文介绍了绑定列表框与多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2010为C#。我有一些数据存在于表

I am using VS2010 for c#. I have some data there in a table

得分(TEAM1,TEAM2,battingteam,bowlingteam,得分)我使用的BindingSource与我的列表框绑定。问题是,我可以只显示一个成员的DisplayMember列表框(例如TEAM1或TEAM2只)的财产。我想要做这样的事情。

Scores(team1,team2,battingteam,bowlingteam,score) I am using bindingSource to bind it with my listBox. The problem is that i can show only one member in DisplayMember property of listbox (e.g. team1 or team2 only). I want to do something like

team1 + " vs " + team2 + ": " + battingteam +" Score: " + score

该怎么做?

推荐答案

我不知道在哪个数据类型,你有你的数据,而是为了例如,如果你有他们在数据表从数据库返回您可以创建列表<串> 对象,并直接向您的列表框迭代的数据表行和格式化的数据创建列表对象的项目,只要你想,并最终绑定列表对象,如下

I don't know in which data type you have your data ... but for example if you have them in DataTable returned from db you can create List<string> object and iterate datatable rows and create items in list object with formatting data as you want and by end bind list object to your listbox directly as below

List<string> teams = new List<string>();
foreach (DataRow dataRow in teamsDataTable.Rows)
{
    teams.Add(dataRow["team1"].ToString() + " vs " + dataRow["team2"].ToString() + ": " + dataRow["battingteam"].ToString() + " Score: " + dataRow["score"].ToString());
}
listBox1.DataSource = teams;

这篇关于绑定列表框与多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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