BindingManagerBase.PositionChanged 事件
============================================
protected void BindControl()
{
/* Create a Binding object for the TextBox control.
The data-bound property for the control is the Text
property. */
Binding myBinding =
new Binding("Text", ds, "customers.custName"); text1.DataBindings.Add(myBinding); // Get the BindingManagerBase for the Customers table.
BindingManagerBase bmCustomers =
this.BindingContext [ds, "Customers"]; // Add the delegate for the PositionChanged event.
bmCustomers.PositionChanged +=
new EventHandler(Position_Changed);
}private void Position_Changed(object sender, EventArgs e)
{
// Print the Position property value when it changes.
Console.WriteLine(((BindingManagerBase)sender).Position);
}
============================================
protected void BindControl()
{
/* Create a Binding object for the TextBox control.
The data-bound property for the control is the Text
property. */
Binding myBinding =
new Binding("Text", ds, "customers.custName"); text1.DataBindings.Add(myBinding); // Get the BindingManagerBase for the Customers table.
BindingManagerBase bmCustomers =
this.BindingContext [ds, "Customers"]; // Add the delegate for the PositionChanged event.
bmCustomers.PositionChanged +=
new EventHandler(Position_Changed);
}private void Position_Changed(object sender, EventArgs e)
{
// Print the Position property value when it changes.
Console.WriteLine(((BindingManagerBase)sender).Position);
}
1。绑定基事件Position_Changed()第一条记录不引起事件,因为记录号变了才发生事件的,
解决这个问题很简单,初始时,自已先调用一次这个事件;不过有没有其它解决办法;
2。有dataGrid时遇到点麻烦,我这个dataGrid1是放在TabControl1中另一个tabPage5中的,
如果不去这个tabPaghe5看一dataGrid1,这个Position_Changed()就是不执行,虽然在
textBox1中看到记录变化,除非去看一下dataGrid1
private void Form1_Load(object sender, System.EventArgs e)
{
oleDbConnection1.Open();
oleDbDataAdapter1.Fill(dataSet1);
DataTable table=dataSet1.Tables[0];
dataGrid1.DataSource=table;
textBox1.DataBindings.Add("Text",table,"姓名");
BindingManagerBase bm =
this.BindingContext [table]; // Add the delegate for the PositionChanged event.
bm.PositionChanged += new EventHandler(Position_Changed);
}
private void Position_Changed(object sender, EventArgs e)
{
// Print the Position property value when it changes.
int p=((BindingManagerBase)sender).Position;
statusBar1.Panels[0].Text=p.ToString();
} private void button1_Click(object sender, System.EventArgs e)
{
DataTable table=dataSet1.Tables[0];
this.BindingContext [table].Position++; // 上一条记录
} private void button2_Click(object sender, System.EventArgs e)
{
DataTable table=dataSet1.Tables[0];
this.BindingContext [table].Position--; // 下一条记录
}