Why when I add a DataGridView from the values in the listbox, you receive the error System.ArgumentNullException?

Error: System.ArgumentNullException: "Value cannot be null.
Parameter name: item"

for (int i = 0; i < filmsDataGridView.RowCount; i++)
 { 
listBox1.Items.Add(filmsDataGridView.Rows[i].Cells[7].Value);
 }
April 3rd 20 at 18:37
1 answer
April 3rd 20 at 18:39
You have an error is due to the fact that filmsDataGridView.Rows[i].Cells[7].Value in some cases, you have a = null and null in insert you cannot CombopBox

5dfad14e21439463486042.jpeg

Convert null's to an empty string so

listBox1.Items.Add(Convert.ToString(filmsDataGridView.Rows[i].Cells[7].Value));

or this:

listBox1.Items.Add(filmsDataGridView.Rows[i].Cells[7].Value?.ToString() ?? String.Empty);
Thank you very much) - Willo commented on April 3rd 20 at 18:42

Find more questions by tags C#Windows Forms