Reputation: 23
How can I refresh whole user entered data in WinForms application? When I click a button I want to clear all my controls i.e. User entered data in windows form application on a button click.
private void button4_Click(object sender, EventArgs e)
{
const string message = "Are you sure you want to clear data";
const string caption = "Please Conform";
var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add(5);
RowNumberSettings();
//Refresh();
}
}
here is my code It clears only Datagridview ...
Upvotes: 0
Views: 1618
Reputation: 12632
I suggest to use MVVM pattern for your forms. Here is some guide.
MVVM provides you a clear separation of view (your forms) and data (what is displayed). You can easily update any part without significant changes in other parts. And you can manipulate any values in the back-end code without knowledge how they are displayed.
In case of usage MVVM you will be able just to recreate the underlying view-model object and all GUI controls will be empty or have their default values (depends on how you implement the view-model).
Upvotes: 0
Reputation: 3625
There are so many ways to do it, but I like to do it like this:
private void ClearTextBoxes()
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).Clear();
else
func(control.Controls);
};
func(Controls);
}
Hope it helps!
Upvotes: 0
Reputation: 8626
Depends upon what data you are having.
Make refresh function and bind all the data in the controls within that function.
On the button click event, just call the refresh function.
void myButton_Click(object sender, RoutedEventArgs e)
{
Refresh()
}
Upvotes: 1
Reputation: 33738
Loop through the Controls
collection (recursively) and handle each control.
Example (in VB):
Sub ResetControls(container as Container)
For Each control As Control In container.Controls
' TODO: Check the control type and reset its value
'
' TODO: If the control is a container, call ResetControls(control)
Next
End Sub
Upvotes: 0