- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcanceledit.cpp
161 lines (138 loc) · 4.8 KB
/
canceledit.cpp
1
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#using <System.Data.dll>
#using <System.Xml.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>
#using <System.Drawing.dll>
usingnamespaceSystem;
usingnamespaceSystem::Drawing;
usingnamespaceSystem::Collections;
usingnamespaceSystem::ComponentModel;
usingnamespaceSystem::Windows::Forms;
usingnamespaceSystem::Data;
namespaceCancelEdit
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public ref classForm1: publicSystem::Windows::Forms::Form
{
private:
System::Windows::Forms::TextBox^ textBox1;
DataView^ myDataView;
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container^ components;
public:
Form1()
{
components = nullptr;
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if ( components != nullptr )
{
delete components;
}
}
private:
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
voidInitializeComponent()
{
this->textBox1 = gcnew System::Windows::Forms::TextBox;
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point( 24, 16 );
this->textBox1->Name = "textBox1";
this->textBox1->Size = System::Drawing::Size( 216, 20 );
this->textBox1->TabIndex = 0;
this->textBox1->Text = "textBox1";
//
// Form1
//
this->ClientSize = System::Drawing::Size( 292, 266 );
array<System::Windows::Forms::Control^>^formControls = {this->textBox1};
this->Controls->AddRange( formControls );
this->Name = "Form1";
this->Text = "Form1";
this->Load += gcnew System::EventHandler( this, &Form1::Form1_Load );
this->ResumeLayout( false );
}
voidForm1_Load( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
SetBinding();
CancelEdit();
EndEdit();
}
//<snippet1>
private:
voidCancelEdit()
{
// Gets the CurrencyManager which is returned when the
// data source is a DataView.
BindingManagerBase^ myMgr = dynamic_cast<CurrencyManager^>(BindingContext[ myDataView ]);
// Gets the current row and changes a value. Then cancels the
// edit and thereby discards the changes.
DataRowView^ tempRowView = dynamic_cast<DataRowView^>(myMgr->Current);
Console::WriteLine( "Original: {0}", tempRowView[ "myCol" ] );
tempRowView[ "myCol" ] = "These changes will be discarded";
Console::WriteLine( "Edit: {0}", tempRowView[ "myCol" ] );
myMgr->CancelCurrentEdit();
Console::WriteLine( "After CanceCurrentlEdit: {0}", tempRowView[ "myCol" ] );
}
voidEndEdit()
{
// Gets the CurrencyManager which is returned when the
// data source is a DataView.
BindingManagerBase^ myMgr = dynamic_cast<CurrencyManager^>(BindingContext[ myDataView ]);
// Gets the current row and changes a value. Then ends the
// edit and thereby keeps the changes.
DataRowView^ tempRowView = dynamic_cast<DataRowView^>(myMgr->Current);
Console::WriteLine( "Original: {0}", tempRowView[ "myCol" ] );
tempRowView[ "myCol" ] = "These changes will be kept";
Console::WriteLine( "Edit: {0}", tempRowView[ "myCol" ] );
myMgr->EndCurrentEdit();
Console::WriteLine( "After EndCurrentEdit: {0}", tempRowView[ "myCol" ] );
}
// </snippet1>
voidSetBinding()
{
// Creates a DataView to be used as a data source. Sets the
// myDataView variable, defined in the form, to the DataView.
// Then binds the TextBox control to the DataView.
DataTable^ myTable = gcnew DataTable( "myTable" );
DataColumn^ myCol = gcnew DataColumn( "myCol" );
myTable->Columns->Add( myCol );
DataRow^ tempRow;
tempRow = myTable->NewRow();
tempRow[ "myCol" ] = "Original Data";
myTable->Rows->Add( tempRow );
myDataView = myTable->DefaultView;
textBox1->DataBindings->Add( gcnew Binding( "Text",myDataView,"myCol" ) );
}
};
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
intmain()
{
Application::Run( gcnew CancelEdit::Form1 );
}