【发布时间】:2014-03-24 00:25:38
【问题描述】:
我正在使用 wpf(c#) 和 mysql 为我的大学做一些项目,并且需要使用 datagrid 来显示有关学生的信息,一旦显示,管理员应该能够插入新信息删除并更新有关学生的某些信息.. 我不希望管理员单击该行,然后在文本框中显示数据并在单击按钮时从那里删除或更新它们...我想让管理员从数据网格中删除并直接在现有数据上的数据网格上键入并点击更新并将其反映在我的数据库中。 我看过很多例子,但没有任何效果,这严重阻碍了我的项目 请帮帮我
我试过了 http://www.nullskull.com/a/1441/wpf-gridview-sample-to-insert-update-and-delete-records.aspx
但这是通过文本框,我需要直接通过 datgrid 进行操作,即我需要一个可编辑的数据网格
现在我显示了数据网格,但是我在数据网格上所做的更改(例如更新一行或删除一行)并未反映在数据库中 我做的代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
namespace testdatagrid
{
public partial class MainWindow : Window
{
DataTable dataTable = new DataTable();
bool changingTitle = true;
public MainWindow()
{
InitializeComponent();
dataTable.RowChanged += new DataRowChangeEventHandler(dataTable_RowChanged);
MessageBox.Show("hi");
}
void dataTable_RowChanged(object sender, DataRowChangeEventArgs e)
{
MessageBox.Show("hi");
if (!changingTitle)
{
UpdateDBIssues();
}
}
private void UpdateDBIssues()
{
MessageBox.Show("hi");
MySqlConnection connection = new MySqlConnection("server=localhost;uid=deepak230890;pwd=xxxx;database=deepak230890;");
string updateString = "UPDATE userdata SET id=?id, username=?username, password=?password, WHERE id=?id";
MySqlCommand updateCommand = new MySqlCommand(updateString, connection);
updateCommand.Parameters.Add("?id", MySqlDbType.Int32, 100, "id");
updateCommand.Parameters.Add("?username", MySqlDbType.VarChar, 100, "username");
updateCommand.Parameters.Add("?password", MySqlDbType.VarChar, 100, "password");
MySqlParameter parameter = updateCommand.Parameters.Add("?id", MySqlDbType.Int32, 10, "id");
parameter.SourceVersion = DataRowVersion.Original;
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.UpdateCommand = updateCommand;
string insertString = "INSERT INTO userdata (id, username, password) " +
"VALUES (?id, ?username, ?password)";
MySqlCommand insertCommand = new MySqlCommand(insertString, connection);
insertCommand.Parameters.Add("?id", MySqlDbType.Int32, 10, "id");
insertCommand.Parameters.Add("?username", MySqlDbType.VarChar, 100, "username");
insertCommand.Parameters.Add("?password", MySqlDbType.VarChar, 100, "password");
adapter.InsertCommand = insertCommand;
MySqlCommand deleteCommand = new MySqlCommand("DELETE FROM userdata WHERE id=?id", connection);
MySqlParameter delParameter = deleteCommand.Parameters.Add("?id", MySqlDbType.Int32, 10, "id");
delParameter.SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand = deleteCommand;
DataTable booksTable = (DataTable)((DataSourceProvider)FindResource("userdata")).Data;
adapter.Update(booksTable);
}
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
这是 mainwindow.xaml.cs 文件
这是 mainwindow.xaml 文件
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="Window"
x:Class="testdatagrid.MainWindow"
xmlns:local="clr-namespace:testdatagrid"
xmlns:s="clr-namespace:System;assembly=mscorlib"
Title="userdata"
Width="680" Height="814">
<Window.Resources>
<ObjectDataProvider x:Key="userdata"
ObjectType="{x:Type local:DatabaseTable}"
MethodName="GetTable">
<ObjectDataProvider.MethodParameters>
<s:String>SELECT * FROM userdata</s:String>
<s:String>username</s:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid DataContext="{StaticResource userdata}">
<DataGrid x:Name="dataTablegrid" ItemsSource="{Binding Mode=OneWay}" SelectionChanged="DataGrid_SelectionChanged" IsSynchronizedWithCurrentItem="True"> </DataGrid>
</Grid>
<!-- Code for UI -->
【问题讨论】: