Translate

> > C# Winform DataGridView on Cell Edit Move to Right Cell

C# Winform DataGridView on Cell Edit Move to Right Cell

Posted on Thursday, July 27, 2017 | No Comments

By Default If you press enter on any cell it will move to Down row cell. so i made some tweak, which will move it right side cell.



       private bool trigger = false;
        private void dgForecast_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex >= 2 && e.ColumnIndex < 33)
            {
                ProcessColumnIndex(true);

            }
        }

        private void dgForecast_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                ProcessColumnIndex(false);
                e.Handled = true;
            }
        }

        private void ProcessColumnIndex(bool flag)
        {
            int row = dgForecast.CurrentCell.RowIndex;

            int col = dgForecast.CurrentCell.ColumnIndex;

            if (col == 32)
            {
                col = 2;
                row++;
            }
            else
            {
                if (flag)
                    trigger = true;
                col++;
            }
            dgForecast.CurrentCell = dgForecast.Rows[row].Cells[col];
        }

        private void dgForecast_SelectionChanged(object sender, EventArgs e)
        {
            if (trigger)
            {
                trigger = false;
                SendKeys.Send("{up}");
            }
        }

Leave a Reply

Powered by Blogger.