简体   繁体   English

WPF DataGrid更新数据库

[英]WPF datagrid update database

I am pretty new in C#. 我在C#中相当陌生。 I was writing a small GUI database application which uses a datagrid to show the table data. 我正在编写一个小型的GUI数据库应用程序,该应用程序使用datagrid来显示表数据。 When I edit a row in the grid press the update button the update function of tableadapter is called and the change is propagated back to DB. 当我在网格中编辑一行时,按更新按钮,将调用tableadapter的更新功能,并将更改传播回DB。 Then I have implemented a handler for row changed event which in turns call the update function of tableadapter. 然后,我为行更改事件实现了一个处理程序,该处理程序又调用了tableadapter的更新功能。 But this time when some value is changed and enter key is pressed the handler is called and the InvalidOperationException is raised with the message "There is already an open DataReader associated with this Command which must be closed first." 但是这一次,当某些值被更改并按下Enter键时,将调用处理程序,并引发InvalidOperationException,并显示消息“已经存在与此Command关联的打开的DataReader,必须首先关闭它”。 but the database is changed anyways. 但是数据库还是被更改了。 How to stop the exception to be raised. 如何停止要引发的异常。 I have multiple active resultsets set to true in the connection string. 我在连接字符串中将多个活动结果集设置为true。

The constructor 构造函数

public WSim(MainWindow h)
{
   InitializeComponent();

   //database connection with strongly typed dataset
   usersAdapter = new testDBDataSetTableAdapters.usersTableAdapter();
   users = usersAdapter.GetUsers();

   users.usersRowChanged +=new testDBDataSet.usersRowChangeEventHandler(users_usersRowChanged);

   this.DataContext = users.DefaultView;
}

The row changed event handler 行更改事件处理程序

private void users_usersRowChanged(object sender, testDBDataSet.usersRowChangeEvent e) 
{
    output.Content = "Row Modified";
    usersAdapter.Update(users);
}

The datagrid xaml DataGrid XAML

<DataGrid Height="200" HorizontalAlignment="Left" Margin="26,249,0,0" Name="userGrid" VerticalAlignment="Top" Width="381" AutoGenerateColumns="True"
              SelectionMode="Extended" SelectionUnit="FullRow"  ItemsSource="{Binding}"
              CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserSortColumns="True"
              CanUserAddRows="True" CanUserDeleteRows="True"/>

As the exception says, you should close the reader first. 例外情况是,您应该先关闭阅读器。 You have not provided enough information and I cannot explain more; 您没有提供足够的信息,我无法解释更多; But See this link . 但是,请参阅此链接 I think it will help you. 我认为它将为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM