Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to Programmatically assign or set a value to a datagrid cell at runtime in C# wpf
Posted
Updated 19-Nov-13 22:14pm
v2

for example i have used a Student class below which implements "INotifyPropertyChanged" as shown below.

C#
public class Student : INotifyPropertyChanged
 {
     private string studentID = string.Empty;

     public string StudentID
     {
         get { return studentID; }
         set
         {
             studentID = value;
             OnPropertyChanged("StudentID");
         }
     }

     private string fName = string.Empty;

     public string FirstName
     {
         get { return fName; }
         set
         {
             fName = value;
             OnPropertyChanged("FirstName");
         }

     }

     private string lName = string.Empty;

     public string LastName
     {
         get { return lName; }
         set
         {
             lName = value;
             OnPropertyChanged("LastName");
         }
     }

     private int totalMark = 0;

     public int TotalMark
     {
         get { return totalMark; }
         set
         {
             totalMark = value;
             OnPropertyChanged("TotalMark");
         }
     }

     #region INotifyPropertyChanged Members

     public event PropertyChangedEventHandler PropertyChanged;

     /// <summary>
     /// On property changed method
     /// </summary>
     /// <param name="propertyName">Property Name</param>
     void OnPropertyChanged(string propertyName)
     {
         if (PropertyChanged != null)
             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }

     #endregion
 }

 public class StudentCollection : ObservableCollection<student>
 {

 }


and a public property for StudentCollection

C#
public StudentCollection studentCollection = new StudentCollection();


and added value for that collection and assigned to the grid

C#
studentCollection.Add(new Student { StudentID = "NK", FirstName = "Navin", LastName = "Kumar", TotalMark = 350 });
            studentCollection.Add(new Student { StudentID = "RK", FirstName = "Ram", LastName = "Kumar", TotalMark = 350 });
            studentCollection.Add(new Student { StudentID = "PK", FirstName = "Prem", LastName = "Kumar", TotalMark = 350 });
            studentDG.ItemsSource = studentCollection;


on some button click i am updating the mark value as below

C#
private void OnUpdateMarkClicked(object sender, RoutedEventArgs e)
        {
            foreach (Student item in studentCollection)
            {
                item.TotalMark += 1;
            }
        }


let me know if it works
 
Share this answer
 
v2
While your question lacks a lot of detail, I'll try to give you some understanding of the way that this is typically done.

The first thing to appreciate is that I'll be talking through this using an architectural pattern known as MVVM (it stands for Model View ViewModel), and it's a very well known pattern. It's used here because WPF works very well with MVVM, as the binding in WPF fits in very nicely with MVVM. So, we have to make some assumptions here:

  1. You have created a class that implements (or inherits from a class that implements) INotifyPropertyChanged. In this class, you have created properties that you will be mapping into the DataGrid, and these properties raise the PropertyChanged event when the value of the property changes.
  2. You have created another class that contains an ObservableCollection of the INotifyPropertyChanged class.
  3. You have assigned an instance of the class in point 2 as the DataContext to the view that contains your DataGrid.

Right, now that we have an understanding of what framework of code we need to put into place, all you need to do is set values in the properties in point 1, and as long as you are raising the PropertyChanged event from the property, your UI will update due to the "magic" of binding.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900