Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good evening,

i am creating an application that reads a database table into a class of objects and then adds it into a list,

however i cant get the data grid to update having issues getting the values from the sqlitedb to mainwindowviewmode,

sqlitedbclass

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();

           GetPumpTypes();



public static ObservableCollection<PumpType> GetPumpTypes()
{
    var list = new ObservableCollection<PumpType>();
    foreach (var p in elfenliedtopfan5)
    {
        list.Add(p);

    }
    return list;
}



then in the mainwindowviewmodel.

public ObservableCollection<PumpType> pumps => new ObservableCollection<PumpType>(SQLITEDB.GetPumpTypes());



i am actioning this on a button click it will run the top code testread()

once that is done it will populate the pumps observablecollection on the mainviewmodel

but it will not update it has 8 entrys but will not update on datagrid,

i have set datagrid up like so

What I have tried:

in the sqlitedb i created 
<pre>        public void runsql()
        {
            updatepumps = pumps;
        }


just updates the


private ObservableCollection<PumpType> updatepumps;

public ObservableCollection<PumpType> UpdatePumps
{
    get { return updatepumps; }
    set
    {
        updatepumps = value;
        OnPropertyChanged();
    }
}


this is something i tryied before to add mainwindowviewmodel
in the sqlite and call the runsql function but still did not work but got rid of that as i did not want to derict call from viewmodel to sqldatabase

the pumptype.cs

public class PumpType
{


    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;
    }




    public int PumpNo { get; set; }
    public int State { get; set; }
    public int Stan { get; set; }
    public int GradeId { get; set; }
    public string GradeName { get; set; }
    public int Ppu { get; set; }
    public int Volume { get; set; }
    public int Cash { get; set; }
    public bool MobileTransaction { get; set; }
    public bool MobileEnabled { get; set; }
}




<Window.DataContext>
        <viewmodel:MainWindowViewModel/>
    </Window.DataContext>
    
    <Grid>
        <Button Content="test"
                Height="30"
                Width="300"
                Command="{Binding TestCommand}" Margin="258,10,242,394">
            
        </Button>


        <DataGrid x:Name="elfen"
                  AutoGenerateColumns="False"
                  Height="340"
                  Margin="4"
                  ItemsSource="{Binding pumps}">


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


i am probably doing this wrong watched tutorials on the binding list to data grid all of those where not using mvvm pattern so it was hard to understand how to accomplish this
hence why i am here any help would be much appreciated

Kind Regards,
elfenliedtopfan5
Posted
Updated 22-Mar-23 13:36pm

1 solution

In your MainViewModel, you have these property:
C#
public ObservableCollection<PumpType> UpdatePumps

public ObservableCollection<PumpType> pumps
    => new ObservableCollection<PumpType>(SQLITEDB.GetPumpTypes());

Data binding is case-sensitive. Make sure you are using Pascal casing and not camel casing[^]. Here:
XML
<DataGrid x:Name="elfen"
    AutoGenerateColumns="False"
    Height="340"
    Margin="4"
    ItemsSource="{Binding pumps}">

Change to:
<pre lang="C#">
public ObservableCollection<PumpType> UpdatePumps

public ObservableCollection<PumpType> Pumps
    => new ObservableCollection<PumpType>();

and
XML
<DataGrid x:Name="elfen"
    AutoGenerateColumns="False"
    Height="340"
    Margin="4"
    ItemsSource="{Binding Pumps}">

Then update Pumps from a method, not an initializer.

Also, why are you doing this?
C#
<pre>public static ObservableCollection<PumpType> GetPumpTypes()
{
    var list = new ObservableCollection<PumpType>();
    foreach (var p in elfenliedtopfan5)
    {
        list.Add(p);

    }
    return list;
}

Instead, do this:
C#
public void InitialzePumps()
{
    Pumps.Clear(); // never renew, always clear otherwise break data binding
    foreach (var pumpData in GetPumpData())
    {
        Pumps.Add(pumpData);

    }
    return list;
}

Then change testread() to:
C#
public IEnumerable<PumpType> GetPumpData()
{

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

	if (result.HasRows)
	{
	   while (result.Read())
	   {
		// Get Fileds here
		
		yield return new PumpType
			(_PumpNo, _State, _stan, _GradeId, 
			 _GradeName, _Ppu, _Volume, _Cash,
			 _MobileTransaction, _MobileEnabled)
	   }
	}
	
	CloseConnection();
}

Now all that you need to do to fill your data is to call
C#
InitialzePumps();
 
Share this answer
 
v5
Comments
elfenliedtopfan5 22-Mar-23 20:58pm    
thank you for your reply once again you seem to the most active on here for replies to my questions however when trying to implement the

public PumpType GetPumpData()
{

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

if (result.HasRows)
{
while (result.Read())
{
// Get Fileds here

//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"]);


yield return new PumpType
(_PumpNo, _State, _stan, _GradeId,
_GradeName, _Ppu, _Volume, _Cash,
_MobileTransaction, _MobileEnabled);


}

CloseConnection();
}
}

i get the following error
CS1624 The body of 'SQLITEDB.GetPumpData()' cannot be an iterator block because 'PumpType' is not an iterator interface type

CS1579 foreach statement cannot operate on variables of type 'PumpType' because 'PumpType' does not contain a public instance or extension definition for 'GetEnumerator'

sorry its not put in code block for some reason it wont let me do this in a comment
Graeme_Grant 22-Mar-23 21:17pm    
Ah, my bad ... I was writing here without testing. I have updated my solution. Sorry about that.
elfenliedtopfan5 22-Mar-23 21:03pm    
should also mention unsure how to hook this up with the viewmodel as all the main code is in the sqlitedb so trying to get pumps.clear()
and pumps.Add()

would have to add a varaible to the sqlitedb.cs
to acess the mainwindowviewmodel.cs to get the Pumps observable collection if that makes sense
Graeme_Grant 22-Mar-23 21:17pm    
You can call it from the constructor.
elfenliedtopfan5 22-Mar-23 22:04pm    
Really stupid question what would be best way to pass through the constructor pump type?

i understand the concept just not sure how to implement it

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