Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project, entering different data on 2 rows(or more than 2 rows) of datagrid (using C #, WPF, database: sql server), after refreshing the datagrid, result:data 2-rows display similar (similar to the last row input)
   data 2-rows display in the database is different (this is expected on the datagrid).
What happened?

Description:
   Input data in two row:
     row 1: MachineId = 1, MachineName = A;
     row 2: MachineId = 2, MachineName = B;
   After refresh datagrid:
     row 1: MachineId = 2, MachineName = B;
     row 2: MachineId = 2, MachineName = B;
   How are do display data the same...:
     row 1: MachineId = 1, MachineName = A;
     row 2: MachineId = 2, MachineName = B;


C#
public partial class Machine : INotifyPropertyChanged
    {
        public int MachineId { get; set; }
        public string MachineName { get; set; }
       
        public event PropertyChangedEventHandler PropertyChanged;
    }

Datagrid:
XML
<DataGrid x:Name="dgMachine" ItemsSource="{Binding MachineList}">            
           <DataGrid.Columns>
                <DataGridTextColumn x:Name="dgtMachineId"  
                    Binding="{Binding MachineId}"
                    Header="MachineId"/>
                <DataGridTextColumn x:Name="dgtMachineName"  
                    Binding="{Binding MachineName}"
                    Header="MachineName" />
            </DataGrid.Columns>
</DataGrid>

Refresh datagrid:
C#
dgMachine.ItemsSource = null;
dgMachine.ItemsSource = ViewModel.MachineList;


What I have tried:

I think the problem in the datagrid, but do not know what is
Posted
Comments
Maciej Los 20-Jan-19 15:58pm    
WPF?
HT Vinh 22-Jan-19 4:24am    
Yes, i am using WPF.
[no name] 21-Jan-19 17:37pm    
Look in "MachineList"; not the grid.

And setting ItemSource to null before "setting" it buys you nothing.
HT Vinh 22-Jan-19 4:23am    
MachineList is List, Code:

public class ViewAViewModel : BindableBase
{
public IList<machine> MachineList { get; }

public ViewAViewModel()
{
MachineList = new ObservableCollection<machine>(new MachineRepo().GetAll());

}
}

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