Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
+		$exception	{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}	System.ArgumentOutOfRangeException


Error is here

row = (DataGridViewRow)roba_uslugeDataGridView.SelectedRows[i].Clone();
               int intColIndex = 0;


Some help?

What I have tried:

private void roba_uslugeDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();

            for (int i = 0; i < roba_uslugeDataGridView.Rows.Count; i++)
            {

                


                row = (DataGridViewRow)roba_uslugeDataGridView.SelectedRows[i].Clone();
                int intColIndex = 0;
               
                foreach (DataGridViewCell cell in roba_uslugeDataGridView.SelectedRows[i].Cells)
                {


                    var ColIndex = dataGridView1.Rows.Add();
                    dataGridView1.Rows[intColIndex].Cells["redni_broj"].Value = intColIndex + 1;
                    dataGridView1.Rows[intColIndex].Cells["bar_kod"].Value = cell.Value.ToString();
                    dataGridView1.Rows[intColIndex].Cells["sifra"].Value = cell.Value;
                    dataGridView1.Rows[intColIndex].Cells["naziv_artikla"].Value = cell.Value;
                    dataGridView1.Rows[intColIndex].Cells["cijena"].Value = cell.Value;
                    dataGridView1.Rows[intColIndex].Cells["kolicina"].Value = "1";


                    foreach (DataGridViewRow g1 in dataGridView1.Rows)
                    {


                        g1.Cells["ukupno"].Value = (Convert.ToDouble(g1.Cells["kolicina"].Value) * Convert.ToDouble(g1.Cells["cijena"].Value)).ToString("0.00");

                    }


                    row.Cells[intColIndex].Value = cell.Value;
                    intColIndex++;
                }
                dataGridView1.Rows.Add(row);
            }
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.Refresh();

        }
Posted
Updated 18-Mar-19 6:56am

1 solution

Simple: not all rows are selected.
i varies from 0 to the number of rows in the DGV. Unless all rows are selected, this will always be larger than the number of items in the SelectedRows collection and you will always get an exception.
 
Share this answer
 
Comments
Goran Bibic 18-Mar-19 13:53pm    
Ok. Recomandioton? Solution? Help
OriginalGriff 18-Mar-19 15:10pm    
How can I? I have no idea why you are using the whole DGV for your loop, and the selection for the access. I think you need to sit down and think about what *exactly* you are trying to do (another thing we don't know) before you start looking for solutions!
Goran Bibic 19-Mar-19 2:17am    
Need to copy row from one datagridview to another
OriginalGriff 19-Mar-19 4:19am    
So?
And what what does that have to do with the problem you are describing? Both the loop and the lien where the error occurs are using the same DGV!
Richard Deeming 21-Mar-19 12:28pm    
If you want to clone the selected rows:
Replace:
i < roba_uslugeDataGridView.Rows.Count

with:
i < roba_uslugeDataGridView.SelectedRows.Count


If you want to clone ALL rows:
Replace:
row = (DataGridViewRow)roba_uslugeDataGridView.SelectedRows[i].Clone();

with:
row = (DataGridViewRow)roba_uslugeDataGridView.Rows[i].Clone();

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