LuckWallace
LuckWallace

Reputation: 133

DataGridView ArgumentOutOfRangeException C#

I'm working with a DataGridView but sometimes when I try to insert a value give me the error ArgumentOutOfRangeException .

For example:

getMaterialesInforme[2, 1].Value = Convert.ToInt32(sumaMP).ToString();

(getMaterialesInforme is a DataGridView)

This error is because cell [2, 1] doesn't exist so is there a way to create this cell before insert the value? and even better, is there a way to create a DataGridView for example from the cell [0, 0] to cell [40, 10]

Thanks in advance!

Upvotes: 0

Views: 91

Answers (1)

SteveZ
SteveZ

Reputation: 195

Try this:

for(int i = 0; i <= 40; i++)
{
    getMaterialesInforme.Columns.Add($"Col{i}", $"Col{i}");
}

for (int i = 0; i <= 10; i++)
{
    getMaterialesInforme.Rows.Add();
}

Upvotes: 1

Related Questions