1

I am reasonably new to C# and am struggling with some very basic things. The examples of using datagrid that I have found have been more complex, I just want a list which I can 'tick off' at the press of a button and add a timestamp to. Any ideas on how best to do this? Currently I have the following which obviously doesn't work.

C#

    public class ListData
    {
        public int Number { get; set; }
        public string Signature { get; set; }
    }

    List<ListData> LoadListData()
    {
        List<ListData> TableInfo = new List<ListData>();
        TableInfo.Add(new ListData()
        {
            Number = 1,
        });
        TableInfo.Add(new ListData()
        {
            Number = 2,
        });
        TableInfo.Add(new ListData()
        {
            Number = 3,
        });
        return TableInfo;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DataGrid1.ItemsSource = LoadListData();
    }

    private void NextRow_Click(object sender, RoutedEventArgs e)
    {
        //update row i here
        //add Signature =  "J Doe " + DateTime.Now,
        // i++
    }

XAML

    <DataGrid Height="200" HorizontalAlignment="Right" Name="DataGrid1" VerticalAlignment="Top" Width="400" />
Cryptomnesia
  • 21
  • 1
  • 4

2 Answers2

3

A simple solution would be to add a Timestamp property (of type DateTime) to your ListData class. Once the button is clicked, you have to loop through your existing collection and add the timestamp value, like this:

var myList = LoadListData();

foreach(var item in myList)
{
   item.Timestamp = DateTime.Now;
}

At the end of the loop, you have to do something like this:

DataGrid1.ItemsSource = myList;
DataGrid1.Items.Refresh(); //to refresh the rows in the DataGrid

A better solution is to use an ObservableCollection of your items and to implement the interface INotifyPropertyChanged in your ListData class, using this pseudo-code:

    public class ListData : INotifyPropertyChanged
    {

    public event PropertyChangedEventHandler PropertyChanged;
    private int _number;

    public int Number 
    { 
    get
    {
      return _number;
    } 
    set
    {
       if(value!= null)
    {
      _number = value;
      OnPropertyChanged("Number");
    }
    }

private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        } 
}


    public ObservableCollection<ListData> MyList {get;set;}

In this way, you will have only to modify the Timestamp property of each item of your collection, without updating manually the DataGrid.

Further info about the ObservableCollection here.

Community
  • 1
  • 1
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
1

This will work for you

      var _row = (ListData)DataGrid1.SelectedItem;

        if (_row != null)
        {
            _row.Signature = "Signed";
            DataGrid1.Items.Refresh();
        }
Tsukasa
  • 6,342
  • 16
  • 64
  • 96