0

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

H.B.
  • 166,899
  • 29
  • 327
  • 400
vvolkgang
  • 471
  • 8
  • 26

1 Answers1

0

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.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I just need a basic CRUD and to know the values of Nota. Searched for the deserialization and found this asnwer http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document/364410#364410 This helps me and (if i'm write) simplify a lot of the CRUD work i have to do (i'm new to WPF). But, if i deserialize the XML i have to do everything in C# right, loosing the XAML data binding features? – vvolkgang Jul 28 '11 at 00:47
  • @NoSila: No, [data binding](http://msdn.microsoft.com/en-us/library/ms752347.aspx) works with all kinds of objects, you just cannot query via XPath anymore. – H.B. Jul 28 '11 at 00:57
  • See this question please-> http://stackoverflow.com/questions/6977397/use-deserialized-xml-data-in-tabcontroller-datagrid – vvolkgang Aug 08 '11 at 19:41