1
votes

I have a datagridview having 5 columns. Columns in datagridview are [Item Code, Description, Quantity,Price & Total].

How it Should Work: When a user enters itemcode, the description and price should fetch from database and displays the result in respective fields. Then the quantity should be entered by user.

WHAT I WANT: I want when user enters value in 1st column(Item Code), the next cell to be selected should be the 3rd column(Quantity). Then if the quantity is entered and user presses the enter button, the next cell selected should be the 1st column(Item Code) of the next row.

PROBLEM: I have tried several codes but its not working. The problem is when an item code is entered in the 1st column(Item Code), the position of the next cell is the 2nd Row of the 3rd column(Quantity). It should move to the 3rd column(Quantity) of the same row i.e the 1st Row.

Below is my code that I have used:

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        currentRow = dataGridView1.CurrentCell.RowIndex;
        currentColumn = dataGridView1.CurrentCell.ColumnIndex;

    }

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {

        if (currentColumn == dataGridView1.Columns.Count - 5)
        {                
            dataGridView1.CurrentCell = dataGridView1[currentColumn + 2, currentRow];

        }
        else
        {
            dataGridView1.CurrentCell = dataGridView1[currentColumn - 2, currentRow + 1];
        }
    }
1
use a foreach loop to determine which row in the datagridview has been selected..MethodMan
how should I do that ? @MethodManAli Muhammad
do a google search on how to find the selected row of a DataGriadView using a foreach loop Cheers..MethodMan
It's too much for a question. Please narrow the question to a single problem. For example, load product name and price when you enter its code in first cell. Or another question can be handling Enter key in DataGridView which you will find some answers for it if you search.Reza Aghaei
Thanks @RezaAghaei I have changed my question now.Ali Muhammad

1 Answers

0
votes

Thanks to this post.

I edited a little bit and here is my code which worked.

 protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        icolumn = dataGridView1.CurrentCell.ColumnIndex;
        irow = dataGridView1.CurrentCell.RowIndex;
        int i = irow;
        if (keyData == Keys.Enter)
        {
            if (icolumn == dataGridView1.Columns.Count - 5)
            {
                   dataGridView1.CurrentCell = dataGridView1[icolumn + 2, irow];
            }
            else
            {
                dataGridView1.CurrentCell = dataGridView1[icolumn - 2, irow + 1];
            }
           return true;
        }


        return base.ProcessCmdKey(ref msg, keyData);
    }