Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am making a windows application in c#. While using datagridview i am getting this error.

C#
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg,System.Windows.Forms.Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                // ON ENTER KEY, GO TO THE NEXT CELL. 
                // WHEN THE CURSOR REACHES THE LAST COLUMN, CARRY IT ON TO THE NEXT ROW.

                if (ActiveControl.Name == "grdsearch")
                {
                    // CHECK IF ITS THE LAST COLUMN
                    if (grdsearch.CurrentCell.ColumnIndex == grdsearch.ColumnCount - 1)
                    {
                        // GO TO THE FIRST COLUMN, NEXT ROW.
                        grdsearch.CurrentCell =
                            grdsearch.Rows[grdsearch.CurrentCell.RowIndex + 1]
                                .Cells[0];
                    }
                    else
                    {
                        // NEXT COLUMN.
                        grdsearch.CurrentCell =
                            grdsearch.Rows[grdsearch.CurrentRow.Index]
                            .Cells[grdsearch.CurrentCell.ColumnIndex + 1];
                    }

                    return true;
                }
                else if (ActiveControl is DataGridViewTextBoxEditingControl)
                {
                    // SHOW THE COMBOBOX WHEN FOCUS IS ON A CELL CORRESPONDING TO THE "QUALIFICATION" COLUMN.
                    if (grdsearch.Columns
                        [grdsearch.CurrentCell.ColumnIndex].Name == "Item_Description")
                    {
                        grdsearch.CurrentCell =
                            grdsearch.Rows[grdsearch.CurrentRow.Index]
                                .Cells[grdsearch.CurrentCell.ColumnIndex + 1];

                        // SHOW COMBOBOX.
                        Show_Combobox(grdsearch.CurrentRow.Index,
                            grdsearch.CurrentCell.ColumnIndex);

                        SendKeys.Send("{F4}");      // DROP DOWN THE LIST.
                        return true;
                    }
                    else
                    {
                        // CHECK IF ITS THE LAST COLUMN.
                        if (grdsearch.CurrentCell.ColumnIndex ==
                            grdsearch.ColumnCount - 1)
                        {
                            // GO TO THE FIRST COLUMN, NEXT ROW.
                            grdsearch.CurrentCell =
                                grdsearch.Rows[grdsearch.CurrentCell.RowIndex + 1]
                                    .Cells[0];
                        }
                        else
                        {
                            // NEXT COLUMN.
                            grdsearch.CurrentCell =
                                grdsearch.Rows[grdsearch.CurrentRow.Index]
                                    .Cells[grdsearch.CurrentCell.ColumnIndex + 1];
                        }
                        return true;
                    }
                }
                else if (ActiveControl.Name == "ComboBox1")
                {
                    // HIDE THE COMBOBOX AND ASSIGN COMBO'S VALUE TO THE CELL.
                    grdsearch.Visible = false;

                    grdsearch.Focus();

                    // ONCE THE COMBO IS SET AS INVISIBLE, SET FOCUS BACK TO THE GRID. (IMPORTANT)
                    grdsearch[grdsearch.CurrentCell.ColumnIndex,
                    grdsearch.CurrentRow.Index].Value = grdsearch.Text;

                    grdsearch.CurrentCell =
                        grdsearch.Rows[grdsearch.CurrentRow.Index]
                            .Cells[grdsearch.CurrentCell.ColumnIndex + 1];
                }
                else
                {
                    SendKeys.Send("{TAB}");
                }
                return true;
            }
            else if (keyData == Keys.Escape)            // PRESS ESCAPE TO HIDE THE COMBOBOX.
            {
                if (ActiveControl.Name == "ComboBox1")
                {
                    
                    HSN_Code.Visible = false;

                    grdsearch.CurrentCell =
                        grdsearch.Rows[grdsearch.CurrentCell.RowIndex]
                            .Cells[grdsearch.CurrentCell.ColumnIndex];

                    grdsearch.Focus();
                }
                return true;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }


What I have tried:

I am getting the error in

C#
if (grdsearch.CurrentCell.ColumnIndex == grdsearch.ColumnCount - 1)
                    {
                        // GO TO THE FIRST COLUMN, NEXT ROW.
                        grdsearch.CurrentCell =
                            grdsearch.Rows[grdsearch.CurrentCell.RowIndex + 1]
                                .Cells[0];
                    }


can you please help me...
I have added column to the datagrid view programatically.
Posted
Updated 4-Jun-19 21:00pm
v2

This happens because CurrentCell.RowIndex + 1 is greater than the highest index of your rows -- in other words, the error happens when you are on the last column AND on the last row, because there is no "next row".

Add an if statement to check if the next row exists and only call that piece of code if it does.
 
Share this answer
 
Comments
MukulMohal 5-Jun-19 3:38am    
yes i did that and updated my code and i was able to navigate from 1 row to another row but that worked till 2nd row. it again throws same exception when trying to navigate from 2nd row to 3rd row.

if (grdsearch.CurrentCell.ColumnIndex == grdsearch.ColumnCount - 1)
{
col = 0;
row++;
// GO TO THE FIRST COLUMN, NEXT
grdsearch.CurrentCell =grdsearch[col,row];
}

int col = grdsearch.CurrentCell.ColumnIndex;
row = grdsearch.CurrentCell.RowIndex;

ADDED
DataRow newrow = table.NewRow();
table.Rows.Add(newrow);
IN MY GRID BIND CODE
Thomas Daniels 5-Jun-19 3:40am    
But also in that code, after row++, you aren't checking if the row actually exists...
MukulMohal 5-Jun-19 3:52am    
no i have not checked if any row exist. As my datagrid was binded to a data source so i can not add rows to the grid directly so i just added the row to datatable. Is there a way i can add row at run time to the datatable
Thomas Daniels 5-Jun-19 3:56am    
I don't know if you can do that, and because I don't really know what you want to do, I also can't say what the right way is to do it. But if you don't want IndexOurOfRange, then do a check before trying to access a row.
MukulMohal 5-Jun-19 4:05am    
by CHECKING Enable Adding property of the data grid view i was able to add new row to the grid view at run time. And i want to make a grid where i can add item details along with quantity its price and total amount.
so there can be multiple rows.
Quote:
'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'

Where are you checking that there is a next row ?
 
Share this answer
 
v2

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