0

I'm fairly new to C# programming. I want to take user input from DataGridView to a DataTable. However, I get ArgumentException from this code

DataTable dd=new DataTable();
       foreach (DataGridViewRow dr in dataGridView1.Rows)
       {
           dd.Rows.Add(dr);
       }

Is there any way I can do to fix it? I'd like to have alternatives to get input from dataGridView1 as well.
edit: forgot to mention, dataGridView1 has one comboBox column.
edit2: the error read "Input array is longer than the number of columns in this table."

0

1 Answer 1

1

Input array is longer than the number of columns in this table.

You have to add a column to DataTable eg: dd.Columns.Add("SomeColumnName"), before you add rows to it.

However, if there's no particular requirement to use DataTable then you should use, for example, List to store the rows. It is a much simpler data structure.

var listOfRows = gridView.Rows.Cast<DataGridViewRow>().ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

ah, I forgot to mention that i will manipulate the data to show it again in another form, so I think I need DataTable. But thanks for pointing out my mistake!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.