Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Problems updating datagrid from collection

$
0
0

I am having trouble getting datagrid to update once collection is updated. Here is my code:

C#:

public partial class MainWindow : Window
    {
        public Student student = new Student();
        public ObservableCollection<Student> studentData = new ObservableCollection<Student>();
        public MainWindow()
        {
            InitializeComponent();

            //Set initial values
            student.Name = "Whatever";
            student.CalculusMarks = 0;
            student.EnglishMarks = 0;
            student.ProgrammingMarks = 0;
            student.Percentage = 0;

            //Read values to collection
            studentData.Add(new Student { Name=student.Name, CalculusMarks = student.CalculusMarks, EnglishMarks = student.EnglishMarks, ProgrammingMarks = student.ProgrammingMarks, Percentage=student.Percentage});

            //Set data context
            datagrid1.DataContext = studentData;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Perform calculation
            student.Percentage = (double)(student.CalculusMarks + student.EnglishMarks + student.ProgrammingMarks) * 100 / 300;
            datagrid1.DataContext = studentData;
            datagrid1.ItemsSource = studentData;
        }

        public class Student : INotifyPropertyChanged
        {
            private string _name;
            private int _calculusMarks;
            private int _englishMarks;
            private int _programmingMarks;
            private double _percentage;

            public string Name
            {
                get
                {
                    return _name;
                }

                set
                {
                    _name = value;
                    OnPropertyChanged("Name");
                }
            }

            public int CalculusMarks
            {
                get
                {
                    return _calculusMarks;
                }

                set
                {
                    _calculusMarks = value;
                    OnPropertyChanged("CalculusMarks");
                }
            }

            public int EnglishMarks
            {
                get
                {
                    return _englishMarks;
                }

                set
                {
                    _englishMarks = value;
                    OnPropertyChanged("EnglishMarks");
                }
            }

            public int ProgrammingMarks
            {
                get
                {
                    return _programmingMarks;
                }

                set
                {
                    _programmingMarks = value;
                    OnPropertyChanged("ProgrammingMarks");
                }
            }

            public double Percentage
            {
                get
                {
                    return _percentage;
                }

                set
                {
                    _percentage = value;
                    OnPropertyChanged("Percentage");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;    

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

Here is my XML:

<Grid><DataGrid Name="datagrid1" AutoGenerateColumns="False" ItemsSource="{Binding}"><DataGrid.Columns>    <DataGridTextColumn x:Name="NameCol" Header="Name" Width="*" Binding="{Binding Path=Name, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/><DataGridTextColumn x:Name="CalculusMarksCol" Header="Calculus" Width="*" Binding="{Binding Path=CalculusMarks, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/><DataGridTextColumn x:Name="EnglishMarksCol" Header="English" Width="*" Binding="{Binding Path=EnglishMarks, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/><DataGridTextColumn x:Name="ProgrammingMarksCol" Header="Programming" Width="*" Binding="{Binding Path=ProgrammingMarks, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/><DataGridTextColumn x:Name="PercentageCol" Header="Percentage" Width="*" Binding="{Binding Path=Percentage, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/></DataGrid.Columns></DataGrid><DockPanel LastChildFill="False"><Button Content="Update" Height="50" Width="75" Margin="5" DockPanel.Dock="Bottom" Click="Button_Click" /></DockPanel></Grid>

Any ideas on what I'm doing wrong???


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>