简体   繁体   中英

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!

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();
}

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