Detect blank row in datagridview

ankit goel 766 Reputation points
2022-12-11T19:46:47.463+00:00

@Jack J Jun , sir This is an another problem that , i am facing in the same datagridview project .
current-cell-focus-problem-after-using-cellendedit.html

As the focus reaches to the last cell of last row and last column , pressing enter key creates a new row and focus moves to that . What i am trying to achieve is if the value of first column of this newly created row gets empty after the user presses enter , the focus should move to the next control instead of next cell in same row .The intention is to get out of datagridview instead of moving to next cell .

  this.SelectNextControl((Control)sender, true, true, true, true);   

but i am out of idea where to detect the blank first cell value so as to use the above code .

Developer technologies .NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-12-12T07:03:37.78+00:00

    @ankit goel , you could try the following code to delete blank row and move the focus to next control.

    private void Form1_Load(object sender, EventArgs e)  
            {  
                DataTable table=new DataTable();  
                table.Columns.Add("Name");  
                table.Columns.Add("Age");  
                table.Columns.Add("Id");  
                table.Rows.Add("test1", 22, 1001);  
                table.Rows.Add("test2", 22, 1002);  
                table.Rows.Add("test3", 22, 1003);  
                dataGridView1.DataSource = table;  
                dataGridView1.AllowUserToAddRows = false;  
            }  
      
            private void dataGridView1_KeyDown(object sender, KeyEventArgs e)  
            {  
                //e.SuppressKeyPress = true;  
                int iColumn = dataGridView1.CurrentCell.ColumnIndex;  
                int iRow = dataGridView1.CurrentCell.RowIndex;  
                if (e.KeyCode == Keys.Enter)  
                {  
                    //to check if the pointer reach last column  
                    if (iColumn == dataGridView1.ColumnCount - 1&&iRow== dataGridView1.RowCount - 1)  
                    {  
                        //executing when line ends  
                        e.Handled = true;  
                        this.SelectNextControl((Control)sender, true, true, true, true);  
      
                    }  
                     
                }  
                 
            }  
    

    Based on my test, it works well.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.