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