近来搞winform开发的, 我通过bindingnavigator绑定到一个bindingsource, bindingsource绑定到dataset1["table1"],,然后用到了 OleDbDataAdapter Adapter1;
OleDbCommandBuilder CommandBuilder1;
目前是能够查询到数据了。
假设里面有字段"f4"现在我想更新bindingnavigator的当前行的字段"f4" = textbox1.text;该怎么写?主要是如何获取当前行的序号i,这样我就可以用以下语句搞定了。
dataset1["table1"].rows[i]["f4"]= textbox1.text;
Adapter1.Update(dataSet1, "table1");
OleDbCommandBuilder CommandBuilder1;
目前是能够查询到数据了。
假设里面有字段"f4"现在我想更新bindingnavigator的当前行的字段"f4" = textbox1.text;该怎么写?主要是如何获取当前行的序号i,这样我就可以用以下语句搞定了。
dataset1["table1"].rows[i]["f4"]= textbox1.text;
Adapter1.Update(dataSet1, "table1");
http://download.csdn.net/detail/cwbugs/4067125
通常的做法是通过绑定到BindingSource的数据显示控件进行更改,最常见的就是DataGridView了其当前行号:DataGridView.CurrentCellAddress.Y
dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["f4"].Value = textbox1.text
或者:dataGridView1.CurrentCell.Value = textbox1.text但不能使用CurrentCellAddress直接对DataSet或DataTable进行赋值,因为DataGridView可能进行了筛选或排序,其单元格的位置索引可能和DataTable内的不一致
DataRowView drv = personBindingSource.Current as DataRowView;
drv["personId"].ToString()
drv["personID"].tostring()="1234567890";
BindingSource有当前行属性,但不常用类似楼主这种情况,通常使用与BindingSource绑定的数据显示控件进行定位和更新,最常见的即是DataGridVeiw
其当前行索引:DataGridView.CurrentCellAddress.Y
dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["f4"].Value = textbox1.text
或者直接:dataGridView1.CurrentCell.Value = textbox1.text但不可以直接使用CurrentCellAddress来对DataSet或DataTable进行更新,因为DataGridView可能进行了筛选或排序,用户选定行的行号 与DataTable内 实际数据行的行号 可能不一致
DataGridView绑定到BindingSource后,
dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["f4"].Value = textbox1.text
会自动更新到BindingSource对应的DataTable内
最后只要
Adapter1.Update(dataSet1)及dataSet1.AcceptChanges()就行了
如果你说要BindingNavigator上面文字框显示出来的那个“位置”值的话,是:
int index = int.Parse(bindingNavigator1.PositionItem.Text);
它无法用于数据更新,因为其实际对应的数据索引无法得到
但它实际是指向其绑定的BindingSource的当前行,更新:
DataRow row = ((DataRowView)bindingSource1.Current).Row;
object[] newValue = (object[])row.ItemArray.Clone();
newValue[1] = textbox1.text; //“1”是字段在DataTable的列索引
row.ItemArray = newValue;
object[] newValue = (object[])row.ItemArray.Clone();
newValue[1] = textbox1.text; //“1”是字段在DataTable的列索引
row.ItemArray = newValue;这个方法可行,如果能够把newValue[1]里面的序号,换成字段的名称,那就perfect了,因为序号可能会变,但是字段名称不会变;加上如果序号太多,数起来也容易出错。
((DataRowView)bindingSource1.Current).Row["validity_d"] = "2000-01-01";
bindingSource1.EndEdit();
Adapter1.Update(dataSet1, "tb_IT_DETCT"); //更新表
这样那些绑定的字段都没有被更新,而只有手动更新的字段.Row["validity_d"] = "2000-01-01";被更新了,,,,,,help!!!!!!!!!
或者干脆所有字段都手动更新
((DataRowView)bindingSource1.Current).Row["字段1"] = "新值1";
((DataRowView)bindingSource1.Current).Row["字段2"] = "新值2";
...