csharp DataGridView-DataTable.GetChanges返回null. [英] csharp DataGridView - DataTable.GetChanges returns null.

查看:149
本文介绍了csharp DataGridView-DataTable.GetChanges返回null.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataTable.GetChanges有时即使返回更改也返回null.

1.在第一行(C,1)中编辑一个单元格,应用更改并将其成功保存到DB.光标返回时移动到单元格(C,2).
这比我想象的更奇怪,因为DataTable changeTable = DataTableTopLevel.GetChanges();返回null,但仍保存
2.在第二行(C,2)中编辑一个单元格,返回键上的值将更改回去.光标也不会移至(C,3),但是光标移至(A,3).
DataTable changeTable = DataTableTopLevel.GetChanges();返回非null,但不保存.
3.在第三行(C,3)中编辑一个单元格,应用更改并将其成功保存到DB.光标返回时移动到单元格(C,4).

DataTable.GetChanges returns null sometimes even though changes have been made.

1.Edit a cell in first row (C,1), change is applied and saves successfully to DB. Cursor moves to cell (C,2) on return.
This is more odd than I thought as DataTable changeTable = DataTableTopLevel.GetChanges(); returns null, but it still saves
2.Edit a cell in second row (C,2), value changes back on return key. Also cursor does not move to (C,3) BUT moves to (A,3).
DataTable changeTable = DataTableTopLevel.GetChanges(); returns non null, but is not saved.
3.Edit a cell in third row (C,3), change is applied and saves successfully to DB. Cursor moves to cell (C,4) on return.

public partial class MeServerMainForm : Form
 {

     string connServer;
     string qryServerTopLevel;
     string qryServerBottomLevel;
     SqlDataAdapter SqlAdapterTopLevel;
     SqlDataAdapter SqlAdapterBottomLevel;
     SqlCommandBuilder SqlCommandBuilderTopLevel;
     SqlCommandBuilder SqlCommandBuilderBottomLevel;
     DataTable DataTableTopLevel;
     DataTable DataTableBottomLevel;
     BindingSource BindingSourceTopLevel;
     BindingSource BindingSourceBottomLevel;

     bool TopLevelCellValueChanged;

     public MeServerMainForm()
     {
         InitializeComponent();
         InitData();
         TopLevelCellValueChanged = false;
     }

     public void InitData()
     {
         //create the connection string
         connServer = "Data Source=192.168.0.11;Initial Catalog=Phil;User Id=Philip;Password=Philip;";

         // TOP LEVEL
         //create the database query
         qryServerTopLevel = @"SELECT
                                             MeID
                                             ,MeTypeID
                                             ,MeTitle
                                             ,CreatedDateTime
                                             ,ReadOnly
                                             ,ParentMeID
                                             ,Question
                                             ,Answer
                                             ,MeScore
                                             ,MeLastTestDate
                                         FROM MyMees
                                         WHERE ParentMeID = 0 ";

         //create an OleDbDataAdapter to execute the query
         SqlAdapterTopLevel = new SqlDataAdapter(qryServerTopLevel, connServer);
         //create a command builder
         SqlCommandBuilderTopLevel = new SqlCommandBuilder(SqlAdapterTopLevel);
         //create a DataTable to hold the query results
         DataTableTopLevel = new DataTable();
         //fill the DataTable
         SqlAdapterTopLevel.Fill(DataTableTopLevel);
         //BindingSource to sync DataTable and DataGridView
         BindingSourceTopLevel = new BindingSource();
         //set the BindingSource DataSource
         BindingSourceTopLevel.DataSource = DataTableTopLevel;
         //set the DataGridView DataSource
         DataGridViewTopLevel.DataSource = BindingSourceTopLevel;

         // Bottom LEVEL
         //create the database query
         qryServerBottomLevel = @"SELECT
                                             MeID
                                             ,MeTypeID
                                             ,MeTitle
                                             ,CreatedDateTime
                                             ,ReadOnly
                                             ,ParentMeID
                                             ,Question
                                             ,Answer
                                             ,MeScore
                                             ,MeLastTestDate
                                         FROM MyMees
                                         WHERE ParentMeID = 2 ";

         //create an OleDbDataAdapter to execute the query
         SqlAdapterBottomLevel = new SqlDataAdapter(qryServerBottomLevel, connServer);
         //create a command builder
         SqlCommandBuilderBottomLevel = new SqlCommandBuilder(SqlAdapterBottomLevel);
         //create a DataTable to hold the query results
         DataTableBottomLevel = new DataTable();
         //fill the DataTable
         SqlAdapterBottomLevel.Fill(DataTableBottomLevel);
         //BindingSource to sync DataTable and DataGridView
         BindingSourceBottomLevel = new BindingSource();
         //set the BindingSource DataSource
         BindingSourceBottomLevel.DataSource = DataTableBottomLevel;
         //set the DataGridView DataSource
         DataGridViewBottomLevel.DataSource = BindingSourceBottomLevel;

         DataTableTopLevel.AcceptChanges();
     }

     private void loadMeTreeToolStripMenuItem_Click(object sender, EventArgs e)
     {

     }

     private void DataGridViewTopLevel_RowLeave(object sender, DataGridViewCellEventArgs e)
     {

         DataTable changeTable = DataTableTopLevel.GetChanges();
         if (changeTable != null)
         {
             SqlAdapterTopLevel.Update(DataTableTopLevel);
             DataTableTopLevel.AcceptChanges();
             DataTableTopLevel.Clear();
             SqlAdapterTopLevel.Fill(DataTableTopLevel);
             TopLevelCellValueChanged = false;

         }




     }




有什么想法吗?
谢谢
Phil




Any ideas?
Thanks
Phil

推荐答案

为什么不使用DataGridViewCellEventArgs?

http://msdn.microsoft.com/en-us/library/system. windows.forms.datagridviewcelleventargs.aspx [ ^ ]

看一下这些话(我会解释一下):
http://msdn.microsoft.com/en-us/library/5dxfaha8.aspx [ ^ ]

祝你好运!
Why not use the DataGridViewCellEventArgs?

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcelleventargs.aspx[^]

Have a look at the remarks (that will explain I think):
http://msdn.microsoft.com/en-us/library/5dxfaha8.aspx[^]

Good luck!


我正在使用DataGridView.RowLeave,我应该使用DataGridView.RowValidated.我相信这是因为可以在RowValidated发生之前触发行离开事件.发生这种情况时,DataGridView.GetChanged()返回null.

例子

I was using DataGridView.RowLeave, I should have used DataGridView.RowValidated. I believe this is because the row leave event can be fired before RowValidated has occurred. When this happens, DataGridView.GetChanged() returns null.

example

private void DataGridViewTopLevel_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            DataTable changeTable = DataTableTopLevel.GetChanges();
            if (changeTable != null)
            {
                SqlAdapterTopLevel.Update(DataTableTopLevel);
            }
        }



谢谢您对Nijboer的帮助,我现在将DataGridViewCellEventArgs用于其他用途.



Thanks for you help E.F. Nijboer, I am now using the DataGridViewCellEventArgs for other uses.


这篇关于csharp DataGridView-DataTable.GetChanges返回null.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆