Editing cells in a DataGridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BRawn
    New Member
    • Jul 2010
    • 28

    #1

    Editing cells in a DataGridView

    Hi guys,

    I'm busy writing an Orders application and one of the columns in the DGV is editable so the user can type in the order amount of the product. I'd like to know if there's a way to set focus to a particular cell so that if there's more than one item in the DGV and the user presses the Enter key, the next cell (Order amount) will be selected. The user must just be able to type in another amount without first having to click on the cell. Ive tried a couple of things already but nothing seems to work properly without the user first having to click on the cell to set focus on the cell and begin typing in the order amount.

    There's no Cell.Focus() method and even though I've tried

    Code:
    DataGridView1.Rows[DataGridView1.CurrentRow.Index].Cells["columnName"].Selected = true;
    it still doesn't work.

    This is what I have on the DGV's KeyUp event:

    Code:
            private void DataGridView1_KeyUp(object sender, KeyEventArgs e)
            {
                    if (e.KeyCode == Keys.Enter)
                    {
    DataGridView1.Rows[DataGridView1.CurrentRow.Index].Cells["columnName"].Selected = true;
                        int columnIndex = int.Parse(DataGridView1.Columns["columnName"].Index.ToString().Trim());
                        int rowIndex = int.Parse(DataGridView1.Rows[DataGridView1.CurrentRow.Index].ToString().Trim());//.Cells["columnName"].Value.ToString().Trim()) - 1;
                        DataGridView1.CurrentCell = DataGridView1[columnIndex, rowIndex];
                        DataGridView1.BeginEdit(true);
                    }
            }
    I've looked on the internet and there's really no other way that really works so I had to throw together my own solution but it doesn't seem to work either. Any light on this would be greatly appreciated :)
  • BRawn
    New Member
    • Jul 2010
    • 28

    #2
    Nevermind guys...I got it :)

    I apologize for the mistake I made here:

    Code:
     int rowIndex = int.Parse(DataGridView1.Rows[DataGridView1.CurrentRow.Index].ToString().Trim());
    It's actually supposed to be:

    Code:
    int rowIndex = DataGridView1.CurrentRow.Index;
    It works as it is...I just made a stupid mistake :$

    Comment

    Working...