0
votes

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.

1

1 Answers

0
votes

I managed to fix my problem. I added the following to my Init code:

DataContext = this;
receptenDg.BeginInit();
db.CreateTable();
receptenDg.SetBinding(ItemsControl.ItemsSourceProperty, new Binding
{
Source = db.dt.Tables[0]

});
receptenDg.AutoGenerateColumns = true;
receptenDg.CanUserAddRows = false;
db.dt.Tables[0].RowChanged += new DataRowChangeEventHandler(Row_Changed);
db.dt.Tables[0].RowDeleted += new DataRowChangeEventHandler(Row_Deleted);
receptenDg.Items.Refresh();
receptenDg.EndInit();

Note the Row_Changed and RowDeleted additions. They call the following functions on a change inside the datagrid:

    #region This function updates the datatable when a row has been altered in the datagrid.
    private void Row_Changed(object sender, DataRowChangeEventArgs e)
    {
        db.Update();
    }
    #endregion
    #region This function updates the datatable when a row has been deleted in the datagrid.
    private void Row_Deleted(object sender, DataRowChangeEventArgs e)
    {
        db.Update();
    }
    #endregion

//db.Update calls a function inside my MySQL Code, which looks like this:

public DataSet Update()
{
    string query = "Select * from Recepten";
    using (MySqlConnection cnn = new MySqlConnection(connectionString))
    using (MySqlCommand cmd4 = new MySqlCommand(query,cnn))
    using(adp = new MySqlDataAdapter(cmd4))
    using (MySqlCommandBuilder myBuilder = new MySqlCommandBuilder(adp))
    {
        cnn.Open();
        adp.Update(dt);
    }
    return dt;
}