0

I have a form with a datagrid that has a list of records from the table. When the user selects one record to edit its value I want to return to the same row (record) in the datagrid and display it as selected. Of course the datagrid should show the new(edited) values. The code in the EditButton does all of it, but the result is not what expected.

    private void zButtonEdit1_Click(object sender, EventArgs e)
    {
        // Store the rowIndex and columnIndex values
        zButtonEdit1.rowIndex = dataGridView1.CurrentRow.Index;
        zButtonEdit1.cellIndex = dataGridView1.CurrentCell.ColumnIndex;
        // Store the Id of the current record
        zButtonEdit1._id_ = Convert.ToInt16( dataGridView1["id_lice", zButtonEdit1.rowIndex].Value.ToString());
        // Open the Editform with required data for that ID 
        Lice frmLice = new Lice(zButtonEdit1.UIB, zButtonEdit1._id_);
        frmLice.ShowDialog();
        // When editing is finished, refresh the grid with new values
        refresh_controls();
        //Set the current record pointer (or row pointer) to the record that was edited
        this.dataGridView1.Rows[zButtonEdit1.rowIndex].Cells[zButtonEdit1.cellIndex].Selected = true;
    }

This is what I get Datagrid on Form Its obvious that I changed only the selection but I have no idea how to change row (record) pointer.

Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
Zed McJack
  • 61
  • 9
  • 1
    You're setting Selected = true on a cell, to set it on the row, call it on the row: `this.dataGridView1.Rows[zButtonEdit1.rowIndex].Selected = true` Reference: http://stackoverflow.com/questions/6265228/selecting-a-row-in-datagridview-programmatically – SilentStorm Mar 09 '17 at 10:28
  • I tried that first, but the result is similar, only now is the row selected not a cell, but the record pointer didnt change. I can post the picture with that if you like or need it. – Zed McJack Mar 09 '17 at 10:30
  • ah, sorry, I misunderstood, I think this is what you want: `dataGridView1.CurrentCell = this.dataGridView1[YourColumn,YourRow];` info: https://msdn.microsoft.com/en-us/library/yc4fsbf5.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 – SilentStorm Mar 09 '17 at 10:38
  • Thank you that worked! How can I upvote your answer there are no options for that in the comments. – Zed McJack Mar 09 '17 at 10:48
  • 1
    @SilentStorm why don't you write the solution as an answer so the Zed McJack can accept it?! – Mong Zhu Mar 09 '17 at 10:53

1 Answers1

1

As requested:

To set the current selected cell:

this.dataGridView1.CurrentCell = this.dataGridView1[YourColumn,YourRow];

Info: https://msdn.microsoft.com/en-us/library/yc4fsbf5.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

SilentStorm
  • 172
  • 1
  • 1
  • 12