向datagrid添加新行 [英] Adding new rows to datagrid

查看:91
本文介绍了向datagrid添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,



我的数据网格遇到问题,看看我将它绑定到ObservableCollection时,集合的结构出现在设计时和之后当datagrid加载时,它会在开头显示空行。



我遇到的第二个问题是我不能在底部的空行添加新的尽管我将CanUserAddRows设置为true而IsReadonly设置为false,是否有任何可能的方法来处理这两个问题?



先谢谢你



SourceCode:我有一个名为ElevesClasses的类,一个Observablecolection定义为:



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 :

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





和CollectionViewSource :





and the CollectionViewSource :

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





然后将datagrid绑定到collectionViewsource,如下所示:





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

<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>





问题是:

1-在设计时这里是屏幕截图网格:http://s8.postimg.org/5o2dogwlx/Datagrid_Design_time.jpg,它甚至在运行时显示一些空行。



2-我想要通过单击行添加行并直接添加它们,但我不能。



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.

推荐答案

您没有为列设置任何绑定。

http://wpftutorial.net/DataGrid.html#manualColumns [ ^ ]

使用绑定的WPF DataGrid格式指南 [ ^ ]



例如:

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:
<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>


这是解决方案,虽然我不满意有了它,我不得不以编程方式定义datagrid的itemssource,而不是在XAML中进行,但仍然没有得到它,但它的工作原理。
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.


这篇关于向datagrid添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆