Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello there. I want to export all selectedRows from a datagridview to a DataTable. When clicking (selecting) on more than 2 rows, the next error appears:

"An exception error of type System.IndexOutOfRangeException occurred in System.Data.dll."

What I have tried:

C#
DataTable table = new DataTable();
            for (int i = 0; i < dataGridView_auswahlen.Rows.Count; i++) {
                if (dataGridView_auswahlen.Rows[i].Selected) {
                    table.Rows.Add( );
                    for (int j = 0; j < dataGridView_auswahlen.Columns.Count; j++) {
                        table.Rows[i][j] = dataGridView_auswahlen[j, i].Value;
                    }
                }
            }
Posted
Updated 10-Nov-16 19:49pm
Comments
[no name] 10-Nov-16 8:51am    
The "IndexOutOfRangeException" is pretty much self explanatory. So, what exactly is your question?
#realJSOP 10-Nov-16 8:59am    
The veracity of your user ID comes immediately into question.

Why would you assume that i is valid for both dataGridView_auswahlen (row source) and table (row destination)?
Since you are only transferring selected rows, unless your selection always starts with the first row and has no gaps, you will always get an exception.
Instead of using an index, create a new row using DataTable.NewRow[^], and add that to your DataTable. You can then use the DataRow object to add your cell values.
 
Share this answer
 
Comments
TheRealProgrammer 10-Nov-16 8:59am    
I understand, but how can I implement that? I can't manage to understand how I can integrate this in my for.
OriginalGriff 10-Nov-16 9:18am    
Create your DataTable then add the columns before the "for" loop - that way you get to give them names which helps.
Then inside your "if", create an instance of a DataRow from the DataTable, and use that instance to set the column values in a loop, pretty much as you do now. Then add the row to the table.

What part of that is difficult? Perhaps I'm explaining it badly...
refer inline comments

C#
DataTable dt = new DataTable(); // create a table for storing selected rows
            var dtTemp = dataGridView1.DataSource as DataTable; // get the source table object
            dt = dtTemp.Clone();  // clone the schema of the source table to new table
            DataTable table = new DataTable();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Selected)
                {
                  var row =   dt.NewRow();  // create a new row with the schema 
                  for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        row[j] = dataGridView1[j, i].Value;
                    }
                  dt.Rows.Add(row);  // add rows to the new table
                }
            }
 
Share this answer
 
Comments
TheRealProgrammer 11-Nov-16 0:51am    
Alright, I understand. The problem is that I need to display all the rows in my dataGridView. From the dataGridView I am selecting what I need and what I selected needs to be cloned into the DataTable. In this example, the dataGridView will only show 1 result, not the whole list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900