1
綁定GetChanges
當綁定到UserControl
的財產(甚至一個簡單的)爲什麼GetChanges會返回一些東西(綁定到屬性時),即使數據表中沒有更改?
我已經做了UserControl
,出於某種原因瞞着我總是返回的東西,當我綁定一個DataColumn
到我的控件的屬性的dataSet1.GetChanges()
總是返回一些東西,即使是綁定到我的控件的列也沒有改變。
GetChanges
總是返回什麼可能的原因是什麼?
這裏有一個簡單的代碼片段重現綁定/ GetChanges問題:
using System;
using System.Data;
using System.Windows.Forms;
namespace BindingBug
{
public partial class Form1 : Form
{
DataSet _ds = new DataSet();
void GetData()
{
var t = new DataTable
{
TableName = "beatles",
Columns =
{
{"lastname", typeof(string)},
{"firstname", typeof(string)},
{"middlename", typeof(string)}
}
};
t.Rows.Add("Lennon", "John", "Winston");
t.Rows.Add("McCartney", "James", "Paul");
_ds.Tables.Add(t);
}
public string Hey { set; get; }
public Form1()
{
InitializeComponent();
var tLastname = new TextBox { Top = 100 };
var tFirstname = new TextBox { Top = 130 };
this.Controls.Add(tLastname);
this.Controls.Add(tFirstname);
GetData();
tLastname.DataBindings.Add("Text", _ds.Tables["beatles"], "lastname");
tFirstname.DataBindings.Add("Text", _ds.Tables["beatles"], "firstname");
// if the following line is commented 2nd:Has Changes == false
this.DataBindings.Add("Hey", _ds.Tables["beatles"], "middlename");
_ds.AcceptChanges();
MessageBox.Show("1st:Has Changes = " + _ds.HasChanges().ToString());
var bDetectChanges = new Button { Top = 160, Text = "Detect Changes" };
bDetectChanges.Click +=
delegate
{
this.BindingContext[_ds.Tables["beatles"]].EndCurrentEdit();
MessageBox.Show("2nd:Has Changes = " + (_ds.GetChanges() != null).ToString());
};
this.Controls.Add(bDetectChanges);
}
}
} //namespace BindingBug