Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagridview: How to set a cell in editing mode?

I need to programmatically 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.

I really want that, with my DGV with several columns, the user can ONLY select and also edit the first two. The other columns are already 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 everytime it has 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...

How can I do this?

like image 926
josecortesp Avatar asked Nov 29 '09 02:11

josecortesp


People also ask

How to make DataGridView cell editable in c#?

You could modify each cell within the column as read only where the cell value is not equal to null or String. Empty. This will allow the user to edit those cells that are blank and protect your data.

How do I edit a data grid in WPF?

DataGrid controls has all that functionality built-in. You can set the properties CanUserAddRows to true to allow user to add rows. DataGrid is editable by default, where each column has an edit control which lets you edit its value.

What is DataGridView in C#?

The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.


2 Answers

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.

like image 132
David Hall Avatar answered Sep 19 '22 17:09

David Hall


private void DgvRoomInformation_CellEnter(object sender, DataGridViewCellEventArgs e) {   if (DgvRoomInformation.CurrentCell.ColumnIndex == 4)  //example-'Column index=4'   {     DgvRoomInformation.BeginEdit(true);      } } 
like image 37
sree ranjith c.k Avatar answered Sep 20 '22 17:09

sree ranjith c.k