when change displayqty not go next row first cell code issue it go next row cell number 4

ahmed salah 3,216 Reputation points
2021-09-26T22:47:33.94+00:00

I work on data grid view I face issue I can't solve it

so please if you can help me please

I have data grid view read barcode it read barcode success and display items data

issue when change quantity and press enter then I need it go to next empty row

on first cell Code to get new barcode by scanner

but this not happen

what happen is cursor go to next row cell number 4

so please How to make first cell focus next row instead of cell number 4

 private void GridTrxInvF_CellValueChanged_1(object sender, DataGridViewCellEventArgs e)
    {


        if (GridTrxInvF.CurrentCell != null && GridTrxInvF.CurrentCell.ColumnIndex == GridTrxInvF.Columns["displayQty"].Index && GridTrxInvF.CurrentCell.Value != null)

        {

            int col = GridTrxInvF.CurrentCell.ColumnIndex;
            int row = GridTrxInvF.CurrentCell.RowIndex;
            int nRows = GridTrxInvF.Rows.Count - 1;
            int nCol = GridTrxInvF.Columns.Count - 1;


            if (nCol == col && nRows == row)
            {
                GridTrxInvF.CurrentCell = GridTrxInvF[0, 0];
            }
            else if (nRows == row)
            {
                GridTrxInvF.CurrentCell = GridTrxInvF[col + 1, 0];
            }
            else
            {




                var row2 = GridTrxInvF.Rows[GridTrxInvF.RowCount - 1];



                GridTrxInvF.CurrentCell = row2.Cells[1];


                GridTrxInvF.CurrentCell.Selected = true;

                GridTrxInvF.BeginEdit(true);

            }

        }


    }

protected override bool ProcessDataGridViewKey(System.Windows.Forms.KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
        _CellEnteredFromEsc = true;
    else
        _CellEnteredFromEsc = false;
    int TimesToSend = 1;
    if (this.CurrentRow == null) return base.ProcessDataGridViewKey(e);
    else if (e.KeyCode == Keys.Enter)
    {

        if (this.CurrentCell.ColumnIndex == _Get_The_Last_Visibale_Index() && this.ValidateRequiredCellsInRow() && this.ValidateInRow() && this.CurrentCell.RowIndex == (this.Rows.Count - 1))
        {
            this.AddNewRow();
        }
        else
        {
            for (int i = this.CurrentCell.ColumnIndex + 1; i < this.Columns.Count; i++)
            {
                if (!this.Columns[i].Visible) continue;





                if (this.Columns[i].Tag != null && (this.Columns[i].Tag as ColumnTag).IsDisabled)
                {
                    if (this[i, this.CurrentCell.RowIndex] == this[this.Columns.Count - 1, this.Rows.Count - 1] && this.ValidateRequiredCellsInRow() && this.ValidateInRow())
                    {
                        this.AddNewRow();
                        return false;
                    }
                    else TimesToSend++;
                }
                else break;
            }

            if (!_StopCellTab)
               SendKeys.Send("{TAB " + TimesToSend + "}");
                                this._CellEnteredFromTab = true;
        }

        return false;
    }


    else if (bIsGridValid)
        return base.ProcessDataGridViewKey(e);
    return false;
}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2021-09-27T10:00:22.273+00:00

    @ahmed salah , you could try the following code to use Sennkeys to go next row first cell.

      private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)  
            {  
                if (e.ColumnIndex == 3)  
                {  
                    SendKeys.Send("{TAB}");  
                    SendKeys.Send("{UP}");  
                     
                }  
      
            }  
    

    Result:

    135513-9999.gif


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.


  2. Karen Payne MVP 35,036 Reputation points
    2021-09-28T02:44:07.57+00:00

    The following out which on the last column edit goes to the next row, first column.

    public class DataGridViewJumper : DataGridView
    {
        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (CurrentRow != null)
                {
                    int currentRow = CurrentRow.Index;
                    if (currentRow >= 0 && CurrentCell.ColumnIndex == ColumnCount -1)
                    {
                        CurrentCell = Rows[currentRow].Cells[0];
                    }
                }
            }
            base.OnKeyUp(e);
        }
    }
    
    0 comments No comments