MVVM Datagrid CellEdit end issue

mion shion 241 Reputation points
2023-03-25T06:12:03.3966667+00:00

Good morning,

i have an issue i am trying to get the cell edit of a data grid item

so i have a database that i grab the values from in its own model class that is here

  public void testread()
        {
   
                string getdb = "select * from PumpState";
                SQLiteCommand elfeninfo = new SQLiteCommand(getdb, testing);
                OpenConnection();
                SQLiteDataReader result = elfeninfo.ExecuteReader();

                if (result.HasRows)
                {
                    while (result.Read())
                    {



                    //var _id = result["ID"];
                    int _PumpNo = Convert.ToInt32(result["PumpNo"]);
                    var _State = Convert.ToInt32(result["State"]);
                    var _stan = Convert.ToInt32(result["Stan"]);
                    var _GradeId = Convert.ToInt32(result["GradeId"]);
                    var _GradeName = Convert.ToString(result["GradeName"]);
                    var _Ppu = Convert.ToInt32(result["Ppu"]);
                    var _Volume = Convert.ToInt32(result["Volume"]);
                    var _Cash = Convert.ToInt32(result["Cash"]);
                    var _MobileTransaction = Convert.ToBoolean(result["MobileTransaction"]);
                    var _MobileEnabled = Convert.ToBoolean(result["MobileEnabled"]);





                    elfennew.PumpNo = _PumpNo;
                    elfennew.State = _State;
                    elfennew.Stan = _stan;
                    elfennew.GradeId = _GradeId;
                    elfennew.GradeName = _GradeName;
                    elfennew.Ppu = _Ppu;
                    elfennew.Volume = _Volume;
                    elfennew.Cash = _Cash;
                    elfennew.MobileTransaction = _MobileTransaction;
                    elfennew.MobileEnabled = _MobileEnabled;



                    elfenliedtopfan5.Add(new PumpType(_PumpNo,
                        _State, _stan, _GradeId, _GradeName, _Ppu, _Volume, _Cash, _MobileTransaction, _MobileEnabled));

                   

                    //ifnull(elfennew.PumpNo  = int.Parse(result["PumpNo"]));
                    //ifnull(elfendataset.ffmpegpath = result["ffmpegPath"].ToString());


                    }

                }
                CloseConnection();
               
            //elfenview = new MainWindowViewModel();
            //elfenview.runsql();
            
        }

and my pumptype.cs

 #region INotifyPropertyChanged Impl
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        public PumpType()
        {

        }

        public PumpType(int PumpNo, int state, int stan, int gradeid, string gradename, int ppu, int volume, int cash, bool mobiletransaction, bool mobileenabled)
        {
            this.PumpNo = PumpNo;
            this.State = state;
            this.Stan = stan;
            this.GradeId = gradeid;
            this.GradeName = gradename;
            this.Ppu = ppu;
            this.Volume = volume;
            this.Cash = cash;
            this.MobileTransaction = mobiletransaction;
            this.MobileEnabled = mobileenabled;
        }


        private int _PumpNo;

        public int PumpNo
        {
            get { return _PumpNo; }
            set 
            {
                _PumpNo = value;
                OnPropertyChanged();
            }
        }


        private int _State;

        public int State
        {
            get { return _State; }
            set 
            {
                _State = value;
                OnPropertyChanged();
            }
        }


        private int _Stan;

        public int Stan
        {
            get { return _Stan; }
            set 
            {
                _Stan = value;
                OnPropertyChanged();
            }
        }


        private int _GradeId;

        public int GradeId
        {
            get { return _GradeId; }
            set 
            {
                _GradeId = value;
                OnPropertyChanged();
            }
        }

        private string _GradeName;

        public string GradeName
        {
            get { return _GradeName; }
            set 
            {
                _GradeName = value;
                OnPropertyChanged();
            }
        }

        private int _Ppu;

        public int Ppu
        {
            get { return _Ppu; }
            set 
            { 
                _Ppu = value;
                OnPropertyChanged();
            }
        }

        private int _Volume;

        public int Volume
        {
            get { return _Volume; }
            set 
            {
                _Volume = value;
                OnPropertyChanged();
            }
        }

        private int _Cash;

        public int Cash
        {
            get { return _Cash; }
            set 
            { 
                _Cash = value;
                OnPropertyChanged();
            }
        }

        private bool _MobileTransaction;

        public bool MobileTransaction
        {
            get { return _MobileTransaction; }
            set 
            { 
                _MobileTransaction = value;
                OnPropertyChanged();
            }
        }

        private bool _MobileEnabled;

        public bool MobileEnabled
        {
            get { return _MobileEnabled; }
            set 
            {
                _MobileEnabled = value;
                OnPropertyChanged();
            }
        }

    }

in the VM

 public MainWindowViewModel()
        {
            elfendb.InitialzePumps();
            Pumps = new ObservableCollection<PumpType>();
            foreach (var item in elfendb.Pumps)
            {
                _Pumps.Add(item);
            }

        }

i add it to another observable collection on the viewmodel,

that is also called pumps,

        private ObservableCollection<PumpType> _Pumps;
        public ObservableCollection<PumpType> Pumps
        {
            get { return _Pumps; }
            private set
            {
                if (value == _Pumps)
                    return;

                _Pumps = value;
                OnPropertyChanged();
            }
        }

i have managed to get the selected pumptype

        private PumpType selectedUser;
        public PumpType SelectedUser
        {
            get => selectedUser;
            set
            {
                var u = value as PumpType;
                selectedUser = u;
                runsql();
                
            }
        }

this returns

the list of the selected item

but if i edit this line say the state from the UI it does not reflect this change

i need it to do this as any changes made to this row i am going to get all the varaibles of the pumptype and send it to update methord that will then read this into the database,

but i cant seem to edit get it to update the values realtime so when i update a value in the cell i want it to pass it to selected user so i can then pass this to another class to be updated but it just saves as 0 that is its default value,

my UI

<DataGrid x:Name="elfen"
                  AutoGenerateColumns="False"
                  Margin="4,67,4,10"
                  CanUserAddRows="False"
                  ItemsSource="{Binding Pumps, UpdateSourceTrigger=PropertyChanged}"
                  CurrentItem="{Binding SelectedUser,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  SelectedItem="{Binding SelectedItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  CurrentCell="{Binding CellInfo, Mode=OneWayToSource}">

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="CellEditEnding">
                    <i:InvokeCommandAction Command="{Binding CellCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
              


            <DataGrid.Columns>
                <DataGridTextColumn Header="PumpNumber" Binding="{Binding Path=PumpNo,Mode=TwoWay}"/>
                <DataGridTextColumn Header="State" Binding="{Binding Path=State,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn Header="Stan" Binding="{Binding Path=Stan,Mode=TwoWay}"/>
                <DataGridTextColumn Header="GradeID" Binding="{Binding Path=GradeId,Mode=TwoWay}"/>
                <DataGridTextColumn Header="GradeName" Binding="{Binding Path=GradeName,Mode=TwoWay}"/>
                <DataGridTextColumn Header="Ppu" Binding="{Binding Path=Ppu,Mode=TwoWay}"/>
                <DataGridTextColumn Header="Volume" Binding="{Binding Path=Volume,Mode=TwoWay}"/>
                <DataGridTextColumn Header="Cash" Binding="{Binding Path=Cash,Mode=TwoWay}" Width="40"/>
                <DataGridTextColumn Header="MobileTransaction" Binding="{Binding Path=MobileTransaction,Mode=TwoWay}" Width="140"/>
                <DataGridTextColumn Header="MobileEnabled" Binding="{Binding Path=MobileEnabled,Mode=TwoWay}" Width="*"/>
            </DataGrid.Columns>

i have tryied all sorts and looked all over for a fix for this i have left the things i have tryied in the code but still am not getting the values in the cells ( that is a observable object of pumptype

any help would be much appreciated because i have no idea what else to try

Thanks in advance elfenliedtopfan5

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
{count} votes