A basic WPF updating DataGrid Example

I was pulling my hair out somewhat, trying to create a DataGrid in WPF, rather than using Winforms. Hopefully, posting my results here can help someone in the future. Leave a comment if they do!

The XAML for the GUI itself. Note that the DataContext property is being set to {Binding Item}. The DataContext is the location that all the auto generated columns come from. If you aren’t auto-generating columns, the DataContext is the root of the Path property that you will need to use.

<Window x:Class="WpfApplication1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="350" Width="737">
    <Grid Width="722">
      <DataGrid AutoGenerateColumns="True" Height="287" 
        HorizontalAlignment="Left"  Margin="12,12,0,0" 
        Name="dataGrid1" VerticalAlignment="Top" 
        Width="258" 
        DataContext="{Binding Item}">
      </DataGrid>
      <Button Content="Button" Height="23" HorizontalAlignment="Left" 
        Margin="520,73,0,0" Name="button1" 
        VerticalAlignment="Top" Width="75" 
        Click="button1_Click" 
      />
    </Grid>
</Window>

Here is a basic C# code snippet. Note that it has 3 classes, Item, IntData, and MainWindow.

  • Item is a basic model. It simply stores an int value. Note: You *MUST* use properties, and NOT fields, so the get and set are required.
  • IntData is an ObservableCollection that stores Item objects. Inheriting from ObservableCollection means whenever an item is added or removed, any GUI “observing” it (get the name now?) will be automatically updated. The listening GUI will NOT be updated if one of the elements changes. That requires implementing an onPropertyChanged() handler.
  • MainWindow is the GUI. It creates an IntData object. The important step is it then sets the ItemsSource property of dataGrid1 to be the created IntData object. The dataGrid1 object has Item set as its data context, so after settings Integers to be the ItemsSource property, the GUI will automatically update to reflect the data it contains.
namespace WpfApplication1
{
    public class Item
    {
        public int value { get; set; }
    }
    public class IntData : ObservableCollection
    {
        public IntData()
            : base()
        {
            Item i = new Item();
            i.value = 56;
            Add(i);
        }
    }
    public partial class MainWindow : Window
    {
        IntData integers;
        public IntData Integers
        {
            get { return integers; }
            set { integers = value; }
        }
        public MainWindow()
        {
            integers = new IntData();

            InitializeComponent();
            dataGrid1.ItemsSource = Integers; // This is an important line
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Item i = new Item();
            i.value = new Random().Next(100);
            integers.Add(i);
        }
    }
}

About samkerr

I'm an eclectic person. I like to dabble in a multitude of things. I'm sure you'll find my blog reflects that.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>