【问题标题】:how to insert update delete within the wpf datagrid itself using mysql i.e editable datagrid如何使用 mysql 在 wpf 数据网格本身中插入更新删除,即可编辑的数据网格
【发布时间】: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 -->

【问题讨论】:

    标签: mysql wpf datagrid


    【解决方案1】:

    这可以做到,但需要一些努力。这不是直截了当的,这意味着您必须将数据源与 Grid 绑定,然后实现删除、更新功能。网上有很多样片。

    要求关注的链接: Example link

    您必须对上面的代码进行更改;

    1. 在主窗口构造函数中执行以下操作

      public MainWindow()
      {
          InitializeComponent();
          DataTable dataTable = (DataTable((DataSourceProvider)FindResource("userdata")).Data;
          dataTable.RowChanged += new DataRowChangeEventHandler(dataTable_RowChanged);
          dataTable.RowDeleted += new DataRowChangeEventHandler(dataTable_RowChanged);
      }
      
    2. 在rowchanged事件中;

      private void dataTable_RowChanged(object sender, DataRowChangeEventArgs e)
      {
          UpdateDBIssues();
      }
      

    我不确定您为什么要进行此检查(!更改标题),在您的情况下,这似乎没有必要,因为您没有像示例中那样使用组合框或列表来更改网格的人口。我测试了代码,它可以工作。所以有了这些改变,你会没事的。如果它仍然不起作用,一次执行一个命令并调试以捕获错误。希望这会有所帮助。

    XAML:类似于

     <Grid DataContext="{StaticResource dataTable}">
        <DataGrid  ItemsSource="{Binding Mode=OneWay}" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True"
                    x:Name="testDataGrid" Margin="0,0,0,0" HorizontalAlignment="Center">
    
          <DataGrid.Columns>
    
                        <!--Databound columns-->
    
           </DataGrid.Columns>
    
        </DataGrid>
    </Grid>
    

    【讨论】:

    • 我尝试了代码,但这里出现错误 objContext = new CompanyEntities();当前上下文中不存在名称 objContext 如何处理?我知道它很蹩脚,但我只是从数据网格开始,所以请帮帮我
    • 粘贴一些代码,更容易找到问题
    • 我已经用代码和答案更新了问题......有没有办法在不使用 ado.net 实体模型的情况下做到这一点......我正在使用 mysql
    • 抱歉,我没有看到您使用的是 mySQL。然后您将无法像在 EF 中那样使用数据上下文。仍然可以通过数据绑定在网格上进行更新、删除、插入。只是我们的连接方式不同。使用此链接 (programming-pages.com/2012/04/13/datagrids-in-wpf)
    • 嘿,我试过了,但在 xaml 中没有成功错误...我已经粘贴了上面的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2013-11-08
    • 2017-02-07
    • 2011-08-13
    • 1970-01-01
    相关资源
    最近更新 更多