I'm trying to make a WPF application centered around a datagrid. I have the following MySQL Table:
+-------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------------+------+-----+---------+----------------+
| id | int(6) unsigned | NO | PRI | NULL | auto_increment |
| DQvalue | varchar(255) | YES | | NULL | |
| DQIndexOf | varchar(255) | YES | | NULL | |
| DIvalue | varchar(255) | YES | | NULL | |
| DIIndexOf | varchar(255) | YES | | NULL | |
| AQvalue | varchar(255) | YES | | NULL | |
| AQIndexOf | varchar(255) | YES | | NULL | |
| AIvalueLoHi | varchar(255) | YES | | NULL | |
| AIIndexOf | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| checkBit | tinyint(1) | NO | | NULL | |
| GND1 | tinyint(1) | NO | | NULL | |
| GND2 | tinyint(1) | NO | | NULL | |
+-------------+-----------------+------+-----+---------+----------------+
This is the DataGrid XAML:
<DataGrid ItemsSource="{Binding Path=., Mode=TwoWay}" x:Name="receptenDg" AutoGeneratedColumns="resize_plugdatagrid" HorizontalAlignment="Left" Height="894" Margin="10,10,0,0" VerticalAlignment="Top" Width="1543" Grid.ColumnSpan="3">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="delete" Content="Delete" Click="Delete_Row"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="{StaticResource PrimaryBrush}"/>
<Setter Property="Foreground" Value="{StaticResource PrimaryFont}" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="BorderBrush" Value="Black"/>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
I also made a Mysql Database class to use throughout my WPF application.
When my WPF application starts, the following code gets initialized:
DataContext = this;
receptenDg.BeginInit();
db.CreateTable();
db.dt.Tables[0].RowDeleted += Row_Deleted;
db.dt.Tables[0].RowChanged += Row_Changed;
receptenDg.SetBinding(ItemsControl.ItemsSourceProperty, new Binding
{
Source = db.dt.Tables[0]
});
receptenDg.Items.Refresh();
receptenDg.EndInit();
This part of the application is working properly. This code properly fills my datagrid with data from my MySQL Database. I have a second window that opens when i click on a button. I input some information, click on a button inside this second window, it adds a new row in my mysql table. Afterwards it closes my second window.
I have tried the following function inside my database class after closing the window like so:
ReceptenPopup recept = new ReceptenPopup();
if (recept.ShowDialog() == true)
{
}
else
{
receptenDg.BeginInit();
db.Update();
db.CreateTable();
receptenDg.SetBinding(ItemsControl.ItemsSourceProperty, new Binding
{
Source = db.dt.Tables[0]
});
receptenDg.Items.Refresh();
receptenDg.EndInit();
}
The code inside the else-statement calls the following functions inside my database class(db is a call to my MySQL class):
public DataSet dt;
public DataSet Update()
{
MySqlCommandBuilder myBuilder = new MySqlCommandBuilder(adp);
adp.Update(dt);
return dt;
}
public DataSet CreateTable()
{
string query = "Select * from Recepten";
using(MySqlConnection conn0 = new MySqlConnection(connectionString))
using(MySqlCommand cmd = new MySqlCommand(query, conn0))
using(adp = new MySqlDataAdapter(cmd))
{
dt = new DataSet();
adp.Fill(dt);
}
return dt;
}
While I can now add new rows to my datagrid, It is not possible to delete a row, which worked previously. This is the function I use to delete a row from my datagrid.
private void Delete_Row(object sender, RoutedEventArgs e)
{
DataRowView row = (DataRowView)receptenDg.SelectedItem;
db.dt.Tables[0].Rows.Remove(row.Row);
//dt.Rows.Remove(row.Row);
db.Update();
receptenDg.Items.Refresh();
}
I previously also used the following functions that I've bound to my init code(see the init code above). I previously worked with a DataTable, but changed over to a DataSet since it was less of a hassle(personally).
private void Row_Changed(object sender, DataRowChangeEventArgs e)
{
updateDataGrid();
}
private void Row_Deleted(object sender, DataRowChangeEventArgs e)
{
updateDataGrid();
}
private void updateDataGrid()
{
receptenDg.BeginInit();
db.Update();
db.CreateTable();
receptenDg.SetBinding(ItemsControl.ItemsSourceProperty, new Binding
{
Source = db.dt.Tables[0]
});
receptenDg.Items.Refresh();
receptenDg.EndInit();
}
When I previously worked with a DataTable, the functions above worked like a charm, but I cannot seem to get the functions working again.
Sorry for my terrible english.