Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am creating purchase in datagridview and i had some input from database by list and some from user with enter key. when user fill data in 1 st line he need to fill another item to 2 line how i can do in .net c # windows application

What I have tried:

i use enter key but when comes to last column in datagridview focus does not come on second line 1 column
Posted
Updated 20-Jul-16 23:14pm
Comments
lukeer 20-Jul-16 9:40am    
[quote]I use enter key but when comes to last column[/quote]
That sounds like you had a working piece of code that changes cell selection on "Enter" input.
If so, what prevents you from selecting next row instead of next column?
If not, how do you get to the last column?

1 solution

Handle code in keydown try like this:

C#
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Enter)
           {
               int columnindex = dataGridView1.CurrentCell.ColumnIndex;
               int rowindex = dataGridView1.CurrentCell.RowIndex;

               if (columnindex < dataGridView1.ColumnCount - 1)
               {
                   columnindex++;
               }
               else
               {
                   columnindex = 0;
                   rowindex++;
               }

               if (rowindex == dataGridView1.RowCount)
                   dataGridView1.Rows.Add();

               dataGridView1.CurrentCell = dataGridView1[columnindex, rowindex];
               e.Handled = true;
           }
       }
 
Share this answer
 

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