Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editable DataGrid in WPF

Tags:

wpf

datagrid

I have a program where I need the user to input data in a DataGrid. I though the simple act of setting the 'CanUserAddRows' would be enough to let the user add new rows, but it seem that it won't cut it.

Is there something else I need to define to make them work? My DataGrid has ComboBoxes and TextBoxes in them so it's pretty common controls.

The code I have so far is this

<dg:DataGrid Name="GridFournisseur" ItemsSource="{Binding}" 
     Margin="423,41,23.5,0" Height="193" VerticalAlignment="Top" 
     CanUserAddRows="True" CanUserDeleteRows="True" IsTabStop="True" RowHeight="12"                         SelectionUnit="CellOrRowHeader">

    <dg:DataGrid.Columns>

        <dg:DataGridComboBoxColumn Header="Fournisseur" Width="*" MinWidth="150"                                                
                                       IsReadOnly="False" />

        <dg:DataGridTextColumn Header="Prix" Width="SizeToHeader" MinWidth="50"
                                       Binding="{Binding Categorie}" 
                                       IsReadOnly="False"/>
        <dg:DataGridTextColumn Header="Délai" Width="SizeToHeader" MinWidth="50"
                                       Binding="{Binding NoPiece}" 
                                       IsReadOnly="False"/>
    </dg:DataGrid.Columns>
</dg:DataGrid>

I just have this DataGrid in which I would like to edit its content and at the beginning it's empty.

like image 711
David Brunelle Avatar asked Sep 14 '09 20:09

David Brunelle


People also ask

How to make DataGrid column editable in wpf?

Set AutoGenerateColumns="False" on the DataGrid and set IsReadOnly="True" for all columns except the column you want to be editable you will set IsReadOnly="False".

How do you make an editable grid in WPF?

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. By default the DataGrid automatically generates columns for every property in your Model, so you don't even have to define it's columns.

How to edit DataGrid cell in 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 DataGrid cell in 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.


1 Answers

It could be any of a number of things. Please take a look at this article.

The problem is most likely that you are binding to a collection view that does not support adding items. I believe that the grid might be expecting a collection view that implements IEditableCollectionView or IBindingList, interfaces that support adding items.

like image 156
Szymon Rozga Avatar answered Oct 18 '22 08:10

Szymon Rozga