通过C#代码访问DataGrid,并处理DataGrid内的数据 [英] Acess to a DataGrid through C# code and manipulate data inside the DataGrid

查看:141
本文介绍了通过C#代码访问DataGrid,并处理DataGrid内的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DataGrid XAML代码

1 - 我想能够添加新行和访问DataGrid的一列的数据,我尝试使用名称支持,但它不工作...

1- I want to be able to add new rows and acess to data of one column of the DataGrid, i've tried using the Name prop but it doesn't work...

2 - 之后,要添加新行,我只需要调用dataGrid.Items的Add方法即可。我用什么方法喂?创建一个具有代表列的答案的类,如回答这里

2- After that, to add a new row i just have to call the Add method of dataGrid.Items ? What do i feed that method with? Create a class with proprieties representing the columns like answered here ?

3 - 我有一个名为Nota的列,我如何访问该列的数据在每一行?

3- I have a column named "Nota", how do i acess to the data of that column in every row?

提前感谢

-A

推荐答案

1。您不能按名称访问网格,因为有多个动态网格,不能有多个字段相同的名称(当然如果可能的话,这不会有帮助)。

1. You cannot access the grid by name as there are multiple dynamic grids and there cannot be multiple fields with the same name (of course that would not help if it was possible).

2。您不能修改项目 ItemsSource 是绑定的,但您可以更改 ItemsSource ,这里是一个如何做的例子:

2. You cannot modify the Items while the ItemsSource is bound, but you can change the ItemsSource, here is an example of how to do that:

<TabControl Name="_tabControl">
    <TabControl.Resources>
        <XmlDataProvider x:Key="data" XPath="GPA/Semestre"
                Source="http://pastebin.com/raw.php?i=JgyYkn4E" />
    </TabControl.Resources>
    <!-- ... -->
    <TabControl.ContentTemplate>
        <DataTemplate>
            <StackPanel>
                <DataGrid Name="localDG" ItemsSource="{Binding XPath=Cadeiras/Cadeira}"
                        AutoGenerateColumns="False" Tag="{Binding XPath=Cadeiras}">
                    <DataGrid.Columns>
                        <!-- ... -->
                    </DataGrid.Columns>
                </DataGrid>
                <Button Content="Add Row" Click="AddRow_Click" Tag="{x:Reference localDG}" />
            </StackPanel>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>





private void AddRow_Click(object sender, RoutedEventArgs e)
{
    var data = _tabControl.Resources["data"] as XmlDataProvider;
    var dg = (sender as FrameworkElement).Tag as DataGrid; //Reference to DataGrid is stored in the Button.Tag
    var cadeiras = dg.Tag as XmlNode; //Used DataGrid.Tag to store parent node of cadeiras so a new cadeira can be added
    var newItem =  data.Document.CreateElement("Cadeira");
    Action<string,string> addProperty = (name, val) =>
        {
            var prop =  data.Document.CreateElement(name);
            prop.InnerText = val;
            newItem.AppendChild(prop);
        };
    addProperty("Activa","1");
    addProperty("Nome","Lorem Ipsum");
    addProperty("Nota","1.3");
    cadeiras.AppendChild(newItem);
}

3。使用类似于2中的方法。您可以查询该物业的物品(例如使用LINQ)

3. Using similar methods as in 2. you can query the items for that property (with LINQ for example)

在旁注中:如果有机会,你会做一些比这更多的操作,我会建议将XML反序列化到正确的CLR对象。文件对象模型的操作是一种痛苦,只是在增加代码行的方面,它们以任何方式,形状或形式都不安全,并且是维护的噩梦。

On a side-note: If there is any chance that you will do just a little bit more manipulation than this i would recommend deserializing the XML to proper CLR-objects. Document object model manipulations are a pain just in terms of additinal lines of code, they are not safe in any way, shape or form, and a nightmare to maintain.

这篇关于通过C#代码访问DataGrid,并处理DataGrid内的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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