Today's exploitation Bindingsource is bound to the control. Program To automatically add the modifier and modify the date when saving the file. This action does not require operator interference. The difficulty is that the rows are modified during the determination and storage.
After testing, it is found that the currentchanged event is triggered only after it occurs.The endedit () Operation of the current item of bindingsource, that is, the endedit () operation cannot be directly triggered after the value is modified in the control. At the same time, the status of endedit () is changed to datarowstate. modified. Code Medium The data source of bindingsource is 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 /**/ ///Update xgid and xgdate, and select the updated row through rowstate. Note that it must be changed to datarowstate. Modified after endedit.
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 ( " Saved successfully! " );
25
26 }
27 Catch (Exception ex)
28 {
29MessageBox. Show (ex. Message );
30
31}
32 }
pay attention to the above content. The bindingsource currentitemchanged event is also triggered after row and endedit (). After the current item is moved in the interface operation, this is triggered only when you navigate to the next line or select other rows. You can also manually endedit () to trigger this event after modifying the value in the control. For example, you can add a button to write bindingsource. endedit () to test that the currentitemchanged event is triggered.