How to add row to DataGrid triggered by a button RRS feed

  • Question

  • I am pulling my hair with this one.  I am still learning XAML and Blend.  I am using the sample data feature of Blend with a DataGrid.

    All I want to do is simple add an additional row to the datagrid when I click on a button.  Or when the row is selected I want to delete this row.  In traditional Visual Studio / C# / Window Forms this is very simple.. But in Blend with XAML I am pulling my hair.

    Can anyone help?


    Tuesday, June 7, 2011 8:49 PM

All replies

  • What kind of datasource do you have in background?
    Wednesday, June 8, 2011 6:06 AM
  • I was using the Sample Database feature of Blend... But I ended up going old school and created the necessary objects then did it all in code as it was taking too long to puts around with XAML and BLEND.. I was hoping that I could do it all in XAML with the Sample Database...

    Here is the code if anyone else gives up on the XAML / Sample Database

    1. Add - using System.Collections.ObjectModel;

    2. Define your class with associated properties that represent your data points (My class name is called DATA)

    private System.Collections.ObjectModel.ObservableCollection<DATA> MyObservableCollection;

     MyObservableCollection = new System.Collections.ObjectModel.ObservableCollection<DATA>();

    MyObservableCollection .Add(new DATA(xxx,xxx,xxx));

    NameOfGrid.ItemsSource = MyObservableCollection ;


    If you want to add a row with a button just add 

    MyObservableCollection .Add(new DATA(xxx.xxx));

    to the Button Click event.. this will add a new row with associated data.. of course this code will change depending on what you want...

    Anyway I am very much still interested in how I can do this with pure XAML and Blend.. Is it even possible?

    Wednesday, June 8, 2011 3:54 PM
  • Ok. So I don't know a solution for you beacuse I'm always adding new rows programatically.
    Thursday, June 9, 2011 5:29 AM
  • Since we are on the topic... what technique do you use to delete rows?  I would like to select the row based on a radio button.  Since the radio button is deeply embedded in the datagrid I simply can not call my radio button object name.... I suspect I must traverse through the Dependency objects - but still dont know how to do this effectively...


    Any recommendations?


    My requirement is - when either a row, radio button or check box is selected I can invoke a click button even on a button within the same screen to delete that row or rows.



    Friday, June 10, 2011 1:23 PM
  • Hi Stanglx,

    In brief, to delete a selected row in datagrid, you should first implement the data structure as ObservableCollection, so the add/delete event can be automatically sent to the datagrid. When you implement the delete button event handler, remove the seleted item from the data structure.

    Thanks, Harry [MSFT]
    Tuesday, June 14, 2011 10:17 PM