views:

2102

answers:

2

Hey guys (this one is about winforms)

I need to programatly set a cell in editing mode. I know that setting that cell as CurrentCell and then call the method BeginEdit(bool), it should happen, but in my case, it doesn't.

What i really want to do is, with my DGV with several columns, the user can ONLY select and also edit the first two. The other columns are already as read only, but the user can select them, and that is what i don't want.

So I was thinking, tell the user to TAB everytimes finished writing on the cell, then select the second cell, then tab again and it select and begin edit the next row's first cell...

Can anybody tell me how to do this??? Thanks in advance

+1  A: 

Well, I would check if any of your columns are set as ReadOnly. I have never had to use BeginEdit, but maybe there is some legitimate use. Once you have done dataGridView1.Columns[".."].ReadOnly = False;, the fields that are not ReadOnly should be editable. You can use the DataGridView CellEnter event to determine what cell was entered and then turn on editing on those cells after you have passed editing from the first two columns to the next set of columns and turn off editing on the last two columns.

0A0D
+3  A: 

Setting the CurrentCell and then calling BeginEdit(true) works well for me.

The following code shows an eventHandler for the KeyDown event that sets a cell to be editable.

My example only implements one of the required key press overrides but in theory the others should work the same. (and I'm always setting the [0][0] cell to be editable but any other cell should work)

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab && dataGridView1.CurrentCell.ColumnIndex == 1)
        {
            e.Handled = true;
            DataGridViewCell cell = dataGridView1.Rows[0].Cells[0];
            dataGridView1.CurrentCell = cell;
            dataGridView1.BeginEdit(true);               
        }
    }

If you haven't found it previously, the DataGridView FAQ is a great resource, written by the program manager for the DataGridView control, which covers most of what you could want to do with the control.

David Hall
Thanks...First i was trying to use the SelectionChange event, and doing some hard (and uggly too) work to avoid stack overflow, since everytimes the selection changes it fires again.But now, i like the most your solution... And thanks +1 for the FAQ. I'm more used to web instead of winforms, but is good to know anyway.Thanks!
josecortesp
This is exactly what I needed. Sort of... :) I was actually trying to update cell contents from outside of the grid that was tied to a datasource. I could put the new values on the screen, but the save button was saving the old values. I needed to put a CurrentCell before and EndEdit() after I updated the values. Your answer got me totally on the right track. Thanks!
BoltBait