vendredi 11 août 2017

C# Select Random Row from Datagridview

I have a C# application where I load song titles from the database to datagridview, Now I already have next and previous buttons

Obviously the next button selects the proceeding row while the previous button selects the preceeding row

I want to create another button that will randomly select any row from the datagridview and prevent selecting the same row twice. This code I found in a thread and altered a bit doesn't seem to work

 private Random rnd = new Random(); private int lastSelectedIndex = -1; 

void RandomRecord() { 
int noRows = dataGridView1.Rows.Count[0]; int index = rnd.Next(noRows); 

while(index == lastSelectedIndex && noRows > 1) { 
index = rnd.Next(noRows);
 } 

lastSelectedIndex = index; 

}




Aucun commentaire:

Enregistrer un commentaire