-3

I want to loop my listbox so it shows a name and then a score next to it.

Is there anyway to do this?

 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";
Habib
  • 219,104
  • 29
  • 407
  • 436
Cain Neal
  • 197
  • 2
  • 4
  • 15
  • you want 2 columns in listbox showing `name` and `score`, right? – Sinatr Mar 15 '13 at 11:14
  • You can refer this http://stackoverflow.com/questions/380451/how-do-i-do-i-loop-through-items-in-a-list-box-and-then-remove-that-item – user2168290 Mar 15 '13 at 11:14
  • Can't you do it in SQL? "SELECT DisplayName = ..., * FROM highscore" then bind to DisplayName... – Adriano Repetti Mar 15 '13 at 11:15
  • 2
    http://stackoverflow.com/questions/15428542/why-i-get-system-data-datarowview-instead-of-real-values-in-my-listbox – Soner Gönül Mar 15 '13 at 11:16
  • 1
    @SonerGönül the answers given in my other question do not work, so i have decided to take an alternative route. – Cain Neal Mar 15 '13 at 11:20
  • @Sinatr Yeah, thats exactly what i am aiming for. Is there a simpler way? Wasnt sure if that would be possible – Cain Neal Mar 15 '13 at 11:23
  • @CainNeal [Bad news](http://stackoverflow.com/questions/2646446/multiple-columns-in-list-box-control) – Sinatr Mar 15 '13 at 12:02
  • can someone please tell me why this was down voted? I have been banned from asking questions, so i aim to rectify any down votes and establish why they were cast. Thanks – Cain Neal May 15 '13 at 20:15

2 Answers2

1

Instead of looping through and adding items in the listbox, you can get the formatted/concatenated result from your query like:

For MySQL, you need to use CONCAT method for concatenation.

SELECT CONCAT(`Name` + ' ' + `Score`) AS `NAMESCORE`, * from highscore;

and then:

lstNames.DisplayMember = "NAMESCORE";
lstNames.ValueMember = "NAMESCORE";
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thanks for answering but i have already attempted that approach and i couldnt get it to work, so i have reverted to what i was going to attempt first, i would only get a return of 'System.Data.Datarowview' in my listbox – Cain Neal Mar 15 '13 at 11:16
  • @CainNeal, Missed that you are using MySql, you need to call CONCAT method to concat columns, try the edited query. – Habib Mar 15 '13 at 11:18
  • @CainNeal put a break point and view the datatable in Data Visualizer in VS, see what you get for concatenated column. – Habib Mar 15 '13 at 11:23
  • i have absolutely no idea how to do that, i can do a breakpoint but thats about it, i am absolutely new to c# and have follwed tutorials to get this far, but cannot find a way to get a stupid highscore db to show on the bloody form. – Cain Neal Mar 15 '13 at 11:39
  • @CainNeal, if you can put a break point, then do that on line `dAdapter.Dispose();`, after that hover your mouse over `dTable`, there you will find options to view the data in visualizer, its pretty useful if you are going to work with databases. – Habib Mar 15 '13 at 11:43
0

Try this

for (int i = 0; i < lstFields.Items.Count; i++)
        {
            var item = lstNames.Items[i] ;
            // Do with current item now
        }
Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83