N
N
nika_fox2019-12-19 02:15:51
C++ / C#
nika_fox, 2019-12-19 02:15:51

Why does System.ArgumentNullException appear when I add values ​​from the DataGridView to the listbox?

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexV, 2019-12-19
@Av-IT

Your error is due to the fact that filmsDataGridView.Rows[i].Cells[7].Value in some cases you have = null, and null cannot be inserted into CombopBox
Always convert nulls to an empty string like
or like this:
listBox1.Items.Add(filmsDataGridView.Rows[i].Cells[7].Value?.ToString() ?? String.Empty);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question