Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I am facing a problem with my datagrid , see when I bind it to an ObservableCollection the structure of the collection appears at Design time and later when the datagrid loads it will display empty rows at the beginning.

The second problem I am having is that I can't the empty row at the bottom to add new items although I set the CanUserAddRows to true and IsReadonly to false, is there any possible way to deal with these two problems ?

Thank you previously

SourceCode : I have a class called "ElevesClasses", and an Observablecolection defined as :

C#
public ObservableCollection<EleveClasses> OCEtudiant = new ObservableCollection<EleveClasses>();


and the CollectionViewSource :

XML
<Window.Resources>
        <CollectionViewSource Source="OCEtudiant" x:Key="ElevesCVS" />
</Window.Resources>


and then the datagrid is bound to the collectionViewsource as follows:

XML
<DataGrid Name="ListElevesGrid" Margin="2,2,2,2" DockPanel.Dock="Top" AutoGenerateColumns="False"
                  CanUserDeleteRows="True"  CanUserAddRows="True" FlowDirection="LeftToRight" 
                  ItemsSource="{Binding Source=OCEtudiant}" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="FamilyName" IsReadOnly="False"/>
                <DataGridTextColumn Header="FirstName" IsReadOnly="False"/>
                <DataGridTemplateColumn Header="Date of Birth" IsReadOnly="False">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <DatePicker />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Place of birth" IsReadOnly="False"/>
                <DataGridTemplateColumn Header="Photo" IsReadOnly="False">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Click="BrowsePhoto" MaxHeight="60" MaxWidth="60">
                                <Image Name="PhotoEleve" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                            </Button> 
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>


The problems are :
1- At design time here is the screen shot of the grid : http://s8.postimg.org/5o2dogwlx/Datagrid_Design_time.jpg , it shows some empty rows even at run time.

2- I want to add rows by clicking on the rows and add them directly , but I can't.
Posted
Updated 20-May-15 4:16am
v2
Comments
Naz_Firdouse 20-May-15 7:57am    
please share your code so that we can help you
Ailane Twofik Muhammed 20-May-15 10:17am    
Please find the question updated with my SourceCode

You've not set up any bindings for your columns.
http://wpftutorial.net/DataGrid.html#manualColumns[^]
Guide to WPF DataGrid formatting using bindings[^]

For example:
XML
<DataGridTextColumn Header="FamilyName" IsReadOnly="False" Binding="{Binding Path=FamilyName}"/>

<DataGridTemplateColumn Header="Date of Birth" IsReadOnly="False">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <DatePicker SelectedDate="{Binding Path=DateOfBirth}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
 
Share this answer
 
Comments
Ailane Twofik Muhammed 21-May-15 4:00am    
It's not question of binding Sir, I just want an empty datagrid to fill some rows than use the rows for something else, binding it to an ObservableCollection was a desperate way from me to get the blank row in the datagrid
Richard Deeming 21-May-15 7:49am    
If you want the grid to have empty rows, why did you list "it shows some empty rows even at run time" as one of the problems you wanted to solve?

And if you don't set up bindings on the columns, then the data you type in won't get pushed back to the source objects.
Ailane Twofik Muhammed 21-May-15 9:45am    
"it shows some empty rows even at run time" ; that's the problem, it shouldn't since it's bound to an empty ObservableCollection neither at runtime nor at design time, I want an empty datagrid and fill the rows at runtime, the only available solution I am having in the internet is through implementing IEditableObject interface or fill the datagrid rows and switch between the cells using Tab or Enter button only, do you have a better solution please ?
Here is the solution although I am not satisfied with it, I had to define the itemssource of the datagrid programatically instead of doing it in XAML, still don't get it though but it works that way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900