我正在使用 c# 尝试使用随机数填充 datagridview,但由于某种原因,我在所有单元格中始终获得相同的值。
public int GenerateRandomNumber()
{
const int minimumNumber = -9;
const int maximumNumber = 15;
var random = new Random();
var randomNumber = random.Next(minimumNumber, maximumNumber);
return randomNumber;
}
gameBoard 是数据网格视图。
private void populateButton_Click(object sender, EventArgs e)
{
CreateGameBoard((int)columnsNumericUpDown.Value,(int)rowsNumericUpDown.Value);
gameBoard.RowTemplate.Height = gameBoard.Height/gameBoard.RowCount;
foreach (DataGridViewRow row in gameBoard.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.Value = GenerateRandomNumber();
}
}
}