private void GetBindingManagerBase() { /* CustomersToOrders is the RelationName of a DataRelation. Therefore, the list maintained by the BindingManagerBase is the list of orders that belong to a specific customer in the DataTable named Customers, found in DataSet1. */ myBindingManagerBase = this.BindingContext[DataSet1, "Customers.CustomersToOrders"]; // Adds delegates to the CurrentChanged and PositionChanged events. myBindingManagerBase.PositionChanged += new EventHandler(BindingManagerBase_PositionChanged); myBindingManagerBase.CurrentChanged += new EventHandler(BindingManagerBase_CurrentChanged); }private void BindingManagerBase_PositionChanged (object sender, EventArgs e) { // Prints the new Position of the BindingManagerBase. Console.Write("Position Changed: "); Console.WriteLine(((BindingManagerBase)sender).Position); }private void BindingManagerBase_CurrentChanged (object sender, EventArgs e) { // Prints the new value of the current object. Console.Write("Current Changed: "); Console.WriteLine(((BindingManagerBase)sender).Current); }private void MoveNext() { // Increments the Position property value by one. myBindingManagerBase.Position += 1; }private void MovePrevious() { // Decrements the Position property value by one. myBindingManagerBase.Position -= 1; }private void MoveFirst() { // Goes to the first row in the list. myBindingManagerBase.Position = 0; }private void MoveLast() { // Goes to the last row in the list. myBindingManagerBase.Position = myBindingManagerBase.Count - 1; }
当你的DataSet成为Form的成员之后,通过Form的BindingContext就可以获取当前DataRow: DataRow dr = ((DataRowView)this.BindingContext[this.Ds,this.Ds.Tables[0].TableName].Current).Rowthis指当前Form Ds是你的DataSet
drv= this.BindingContext[数据源].Current as DataRowView DataRow myDataRow = drv.Row; 这个myDataRow就是当前绑定记录
BindingManageBase bmb=Control.BindingContext[dataSource,dataMember]; DataRowView drv=bmb.Current as DataRowView; //所管理的当前DataRowView int record=bmb.Posiont; //当前指向
{
oleDbConnection1.Open(); // 已设好连接串与SQL语句
oleDbDataAdapter1.Fill(dataSet1);
DataTable table=dataSet1.Tables[0];
dataGrid1.DataSource=table;
textBox1.DataBindings.Add("Text", table, "单位"); // 字段名
}
上面的程序是dataGrid1显示的是一个Access2000的表:“单位”
文本编辑框textBox1与字段"单位“已绑定,
可以发现,可以改变dataGrid1的当前记录位置,
随着 dataGrid1当前记录的变化,textBox1字段值同步的跟着变化,
肯定有当前记录的概念,我想!
{
/* CustomersToOrders is the RelationName of a DataRelation.
Therefore, the list maintained by the BindingManagerBase is the
list of orders that belong to a specific customer in the
DataTable named Customers, found in DataSet1. */
myBindingManagerBase =
this.BindingContext[DataSet1, "Customers.CustomersToOrders"]; // Adds delegates to the CurrentChanged and PositionChanged events.
myBindingManagerBase.PositionChanged +=
new EventHandler(BindingManagerBase_PositionChanged);
myBindingManagerBase.CurrentChanged +=
new EventHandler(BindingManagerBase_CurrentChanged);
}private void BindingManagerBase_PositionChanged
(object sender, EventArgs e)
{
// Prints the new Position of the BindingManagerBase.
Console.Write("Position Changed: ");
Console.WriteLine(((BindingManagerBase)sender).Position);
}private void BindingManagerBase_CurrentChanged
(object sender, EventArgs e)
{
// Prints the new value of the current object.
Console.Write("Current Changed: ");
Console.WriteLine(((BindingManagerBase)sender).Current);
}private void MoveNext()
{
// Increments the Position property value by one.
myBindingManagerBase.Position += 1;
}private void MovePrevious()
{
// Decrements the Position property value by one.
myBindingManagerBase.Position -= 1;
}private void MoveFirst()
{
// Goes to the first row in the list.
myBindingManagerBase.Position = 0;
}private void MoveLast()
{
// Goes to the last row in the list.
myBindingManagerBase.Position =
myBindingManagerBase.Count - 1;
}
MessageBox.Show( ((DataRowView)this.BindingContext[ds,"SysUser"].Current)[0].ToString() );2).
MessageBox.Show( ds.Tables["SysUser"].Rows[this.BindingContext[ds,"SysUser"].Position][0].ToString() );
这时dataSet1.Tables[0];就是当前记录
DataRow row = dataSet1.Tables[0].Rows[0]
这时dataSet1.Tables[0].Rows[0]就是当前记录
当前记录只是个相对的概念--我的拙见
DataRow dr = ((DataRowView)this.BindingContext[this.Ds,this.Ds.Tables[0].TableName].Current).Rowthis指当前Form
Ds是你的DataSet
DataRow myDataRow = drv.Row;
这个myDataRow就是当前绑定记录
DataRowView drv=bmb.Current as DataRowView; //所管理的当前DataRowView
int record=bmb.Posiont; //当前指向
MessageBox.Show(dataGrid1.CurrentCell.RowNumber.ToString());
dataGrid1.CurrentRowIndex +=1;DataTable估计也有一个
在数组中,没有当前元素的概念,但是在遍历数组时有。
都是DataTable对象,所以我肯定这当前记录是存在的,就在DataTable内.
{
oleDbConnection1.Open(); // 已设好连接串与SQL语句
oleDbDataAdapter1.Fill(dataSet1);
DataTable table=dataSet1.Tables[0];
dataGrid1.DataSource=table;
textBox1.DataBindings.Add("Text", table, "单位"); // 字段名
}
在这段代码里,textBox1的DataSource是什么?没有吧,这是为什么?
因为文本框只能绑定一个数据,所以只有DataBinding而不能有DataSource。那么DataSource是怎么来的?楼主自己好好想想吧。顺便清理一下自己的概念。
这一句有table参数呀
为什么DataGrid1移动时,textBox1也跟着动的,是什么把他们牵在一起呢?
这两者的共同点就是table呀
再想想,欢迎讨论!
MessageBox.Show(dataGrid1.CurrentCell.RowNumber.ToString());"
真有这种书吗?一定是国内作者!那是作者在骗钱,如果Row有删除,dataGrid1.CurrentCell.RowNumber还同Row号相等吗?
int pos = this.DataGrid1.BindingContext[this.Ds,this.Ds.Tables[0].TableName].Position;
这才是数据的当前位置,dataGrid1.CurrentCell.RowNumber只是DataGrid的当前行,不完全相等."为什么DataGrid移动记录时,被绑定的TextBox1同步地也变化记录,这两者的数据源
都是DataTable对象,所以我肯定这当前记录是存在的,就在DataTable内."那时因为Form的CurrencyManager能够自动管理数据,保持数据同步.
你坚持要的当前行就是它:
DataRow dr = ((DataRowView)this.DataGrid1.BindingContext[this.Ds,this.Ds.Tables[0].TableName].Current).Row
同我前面的代码其实是一回事,因为DataGrid继承自Form
楼上的话我再看看
// dataGrid1.BindingContext[table].Position 当前记录位置
// textBox1.BindingContext[table].Position 当前记录位置
if (dataGrid1.BindingContext[table]==textBox1.BindingContext[table])
MessageBox.Show("==");
这才明白了,两个控件绑定的table对象是相同的,所以,它们是同步的,
这个"当前记录"是绑定管理器的属性,并不在table中,
谢谢几位高手的邦忙,我才学C#一星期,不太了解它的数据库对象。
Form1.BindingContext[table].Position 这也是