简体   繁体   中英

How do i generate random 1 digit numbers and then add them in each cell of a datagridview in c# window form application?

the current nested for loop is populating all the cells with the same value not different values

{
  int numbers;
  Random numberGenerator= new Random();
  numbers = numberGenerator.Next(0,9);

   For(int i=0; i< dgvGuess.Columns.Count; i++)
   {
         For(int u=0; u < dgvGuess.Rows.Count-1; u++)
         {
              dgvGuess.Rows[k].Cells[u].Value= numbers;
         }
   }
}

You have to generate a new random number with every time you iterate over the cell

 for(int i=0; i< dgvGuess.Columns.Count; i++)
 {
       for(int u=0; u< dgvGuess.Rows.Count-1; u++)
       {
            dgvGuess.Rows[k].Cells[u].Value= numberGenerator.Next(0,9);
       }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM