我想循环我的列表框,以便显示一个名称,然后是旁边的分数。
有没有这样做?
MySqlConnection myConn = new MySqlConnection(connStr);
DataTable dTable = new DataTable();
string squery = "SELECT * FROM highscore";
MySqlDataAdapter dAdapter = new MySqlDataAdapter(squery, myConn);
dAdapter.Fill(dTable);
dAdapter.Dispose();
lstNames.DataSource = dTable;
lstNames.DisplayMember = "Name";
lstNames.ValueMember = "Name";
答案 0 :(得分:1)
您可以从查询中获取格式化/连接结果,而不是在列表框中循环并添加项目,而不是:
对于MySQL,您需要使用CONCAT方法进行连接。
SELECT CONCAT(`Name` + ' ' + `Score`) AS `NAMESCORE`, * from highscore;
然后:
lstNames.DisplayMember = "NAMESCORE";
lstNames.ValueMember = "NAMESCORE";
答案 1 :(得分:0)
试试这个
for (int i = 0; i < lstFields.Items.Count; i++)
{
var item = lstNames.Items[i] ;
// Do with current item now
}