Acess to a DataGrid through C# code and manipulate data inside the DataGrid

懵懂的女人 提交于 2019-12-25 03:57:19

问题


My DataGrid XAML code

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- 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- I have a column named "Nota", how do i acess to the data of that column in every row?

Thanks in advance,

-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. 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. Using similar methods as in 2. you can query the items for that property (with LINQ for example)


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.



来源:https://stackoverflow.com/questions/6825438/acess-to-a-datagrid-through-c-sharp-code-and-manipulate-data-inside-the-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!