[英]Having trouble deleting rows and changing data in WPF DataGrid (using MySQL)
I'm trying to make a WPF application centered around a datagrid. 我正在尝试使WPF应用程序围绕一个数据网格。 I have the following MySQL Table: 我有以下MySQL表:
+-------------+-----------------+------+-----+---------+----------------+
| 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 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. 我还制作了一个Mysql数据库类,以在整个WPF应用程序中使用。
When my WPF application starts, the following code gets initialized: 当我的WPF应用程序启动时,以下代码被初始化:
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. 这段代码用MySQL数据库中的数据正确地填充了datagrid。 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. 我输入一些信息,在第二个窗口中单击一个按钮,它在mysql表中添加了新行。 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): else语句中的代码在我的数据库类中调用以下函数(db是对我的MySQL类的调用):
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). 我以前使用过DataTable,但是由于它不那么麻烦(个人),所以改用了DataSet。
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. 当我以前使用DataTable时,上面的功能像个魅力一样起作用,但是我似乎无法使这些功能再次起作用。
Sorry for my terrible english. 对不起,我的英语不好。
I managed to fix my problem. 我设法解决了我的问题。 I added the following to my Init code: 我在Init代码中添加了以下内容:
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. 请注意Row_Changed和RowDeleted添加。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.