今天利用BindingSource绑定到控件做程序,想让在保存是自动添加修改人和修改日期。这个动作不想让操作人员干扰。难点在于判定保存时,那些行做了修改。
经测试,发现CurrentChanged 事件 ,发生后才引发BindingSource的当前项的endedit()操作,也就是在控件中修改值后不能直接引发endedit()操作。同时发现endedit()后行状态才变为DataRowState.Modified。代码中BindingSource的数据源是this.rhxsglDataSet.T_zhenceline.
经测试,发现CurrentChanged 事件 ,发生后才引发BindingSource的当前项的endedit()操作,也就是在控件中修改值后不能直接引发endedit()操作。同时发现endedit()后行状态才变为DataRowState.Modified。代码中BindingSource的数据源是this.rhxsglDataSet.T_zhenceline.
1
private void ubtline_btsaveClick(object sender, System.EventArgs e)
2
{
3
try
4
{
5
this.Validate();
6
this.t_zhencelineBindingSource.EndEdit();
7
///对xgId,xgdate进行更新,选择更新过的行通过rowstate来判定,注意一定要在endedit之后它才变为DataRowState.Modified
8
int i = 0;
9
int Linecount = 0;
10
if (this.rhxsglDataSet.T_zhenceline != null)
11
Linecount = this.rhxsglDataSet.T_zhenceline.Rows.Count;
12
while (i < Linecount )
13
{
14
if (this.rhxsglDataSet.T_zhenceline.Rows[i].RowState==DataRowState.Modified)
15
{
16
this.rhxsglDataSet.T_zhenceline.Rows[i]["xgid"] = My_classes.C_user.LoginUserId;
17
this.rhxsglDataSet.T_zhenceline.Rows[i]["xgdate"] = DateTime.Now;
18
}
19
20
i = i + 1;
21
}
22
23
this.t_zhencelineTableAdapter.Update(this.rhxsglDataSet.T_zhenceline);
24
MessageBox.Show("保存成功!");
25
26
}
27
catch (Exception ex)
28
{
29
MessageBox.Show(ex.Message);
30
31
}
32
}
注意上面的内容。还有BindingSource的CurrentItemChanged事件也是在row,endedit()后才触发的,在界面操作上就是当前项移动后,即导航到下一行,或者选择其他行时才触发。或者在控件中修改值后,手动endedit()就可以触发此事件。比如你加个按钮在按钮中写入BindingSource.endedit()就可以测试到触发了CurrentItemChanged事件。

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32
