I want to add my specified data to column but i get error “Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index”
private void Button1Click(object sender, EventArgs e)
{
var result = _transactionService.GetTransactionReport(DateTime.Now.AddDays(-2), DateTime.Now);
foreach (var transaction in result)
{
for (int r = 0; r < result.Count; r++)
{
dataGridView1.Rows[r].Cells[0].Value = transaction.Number;
dataGridView1.Rows[r].Cells[1].Value = transaction.DateTime;
dataGridView1.Rows[r].Cells[1].Value = transaction.Customer.Name;
}
}
}
how to add data to row and column?
Is the error being throw on this line:
I believe you’re getting that error because you are referencing a row in your DataGridView that does not exist. There a number of ways to add a row to a DataGridView. You can use the
DataGridViewRowCollection.Addmethod to add new row and populate it with values from one of your transaction objects:Note that this code assumes your DataGridView already has at least three columns.