Click here to Skip to main content
15,120,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have added columns to the data grid view programtically. and when i am trying to move from one cell to another cell on the enter click its navigation is not proper.
here is my code
public void bind_grid()
        {
            itemgrid1.ColumnCount = 5;
            itemgrid1.AutoGenerateColumns = false;

            itemgrid1.Columns[0].HeaderText = "Item_Description";
            itemgrid1.Columns[0].Name = "Item_Description";
            itemgrid1.Columns[0].Visible = true;
            itemgrid1.Columns[0].Width = 278;
            itemgrid1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            //itemgrid1.Columns["Item_Description"].DisplayIndex = 0;

            itemgrid1.Columns[1].HeaderText = "Quantity";
            itemgrid1.Columns[1].Name = "Quantity";
            itemgrid1.Columns[1].Visible = true;
            itemgrid1.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //itemgrid1.Columns["Item_Description"].DisplayIndex = 2;

            itemgrid1.Columns[2].HeaderText = "Converstion_Type";
            itemgrid1.Columns[2].Name = "Converstion_Type";
            itemgrid1.Columns[2].Visible = true;
            itemgrid1.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //itemgrid1.Columns["Item_Description"].DisplayIndex = 4;

            itemgrid1.Columns[3].HeaderText = "Rate";
            itemgrid1.Columns[3].Name = "Rate";
            itemgrid1.Columns[3].Visible = true;
            itemgrid1.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //itemgrid1.Columns["Item_Description"].DisplayIndex = 5;

            itemgrid1.Columns[4].HeaderText = "amount";
            itemgrid1.Columns[4].Name = "amount";
            itemgrid1.Columns[4].Visible = true;
            itemgrid1.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            itemgrid1.Columns[4].ReadOnly = true;
            //itemgrid1.Columns["Item_Description"].DisplayIndex = 6;

            itemgrid1.Columns.Add(Units);
            itemgrid1.Columns.Add(HSN_Code);

            itemgrid1.GridColor = Color.FromArgb(211, 225, 229);
            itemgrid1.BackgroundColor = Color.Wheat;

            itemgrid1.RowsDefaultCellStyle.BackColor = Color.AliceBlue;
            itemgrid1.RowsDefaultCellStyle.SelectionBackColor = Color.CornflowerBlue;
            itemgrid1.RowsDefaultCellStyle.SelectionForeColor = Color.White;
            itemgrid1.Rows.Add(new DataGridViewRow());

        }


What I have tried:

<pre>private void itemgrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                int iColumn = itemgrid1.CurrentCell.ColumnIndex;
                int iRow = itemgrid1.CurrentCell.RowIndex;
                if (iColumn == itemgrid1.Columns.Count - 1)
                {
                    itemgrid1.Rows.Add(new DataGridViewRow());
                    itemgrid1.CurrentCell = itemgrid1[0, iRow + 1];
                }
                else
                    itemgrid1.CurrentCell = itemgrid1[iColumn + 1, iRow];
            }
        }

private void itemgrid1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    LastColumnIndex = e.ColumnIndex;
    LastRowIndex = e.RowIndex;

    if (itemgrid1.ColumnCount - 1 == e.ColumnIndex)  //if last column
    {
        //KeyEventArgs forKeyDown = new KeyEventArgs(Keys.Enter);
        itemgrid1.Rows.Add(new DataGridViewRow());
        itemgrid1.CurrentCell = itemgrid1[0, e.RowIndex + 1];
    }
    else
    {
        if (e.RowIndex > 0)
            itemgrid1.CurrentCell = itemgrid1[e.ColumnIndex + 1, e.RowIndex];
        else
        {
            SendKeys.Send("{UP}");
            SendKeys.Send("{RIGHT}");
        }
    }

}
Posted
Updated 10-Jun-19 20:58pm

1 solution

Basically, don't.

There are standard functions which users expect keys to perform - and ENTER is a key that has a specific role: it accepts input and sends it off for processing. Moving between cells is the function of the TAB key - and you mess with that at your peril. Making your app non-standard will upset users who have to remember that "your app works differently" and that will colour their entire experience; their whole attitude to your app.

Subverting ENTER to do the job of TAB is a bad idea from a standardised UI POV - and will often prevent users wanting to use your app at all!
   
Comments
MukulMohal 11-Jun-19 2:22am
   
but what if it is requirement of the user?

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