Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid - Set a cell into edit mode programmatically

Tags:

wpf

datagrid

  • I have a WPF DataGrid that shows some data records (bounded to an ObservableCollection).

  • When the user clicks "Edit" button, the currend selected row should move into edit-mode (As if the user double-clicked this row).

How do I do that?

like image 463
Nir Avatar asked Oct 27 '09 07:10

Nir


People also ask

How to edit cell in DataGrid WPF?

By default, you can edit items directly in the DataGrid. The user can enter edit mode in a cell by pressing F2 key or double tapping on a cell. Alternatively, you can set the DataGridColumn. IsReadOnly property to true to disable editing in specific columns of the DataGrid.

How to edit cell in DataGridView c#?

By default, users can edit the contents of the current DataGridView text box cell by typing in it or pressing F2. This puts the cell in edit mode if all of the following conditions are met: The underlying data source supports editing. The DataGridView control is enabled.

What is the difference between grid and DataGrid in WPF?

A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.

Can user add rows DataGrid WPF?

WPF DataGrid (SfDataGrid) provides built-in row called AddNewRow. It allows user to add a new row to underlying collection. You can enable or disable by setting SfDataGrid.


2 Answers

Assuming WPF:

<DataGrid x:Name="dg".... />

Then this code will work:

dg.CurrentCell = new DataGridCellInfo(dg.Items[i], dg.Columns[j]);
dg.BeginEdit();
like image 135
alkk Avatar answered Sep 25 '22 16:09

alkk


Here is the documentation of the WPF DataGrid on MSDN. The BeginEdit method seems to be what you are looking for.

PS: I don't know if this is suitable for your application, but many DataGrid users find Single-Click Editing useful.

like image 34
Heinzi Avatar answered Sep 22 '22 16:09

Heinzi