C# - Binding FormClosing event to cause Form refresh - c#

I am having some difficulty getting a form to refresh when another form is closed. This is what I have so far, but it doesn't seem to trigger the refresh. I am very to new to programming, so any assistance is appreciated!
private void button2_Click(object sender, EventArgs e)
{
AddNewCourse ANCform = new AddNewCourse();
ANCform.FormClosing += new FormClosingEventHandler(this.ANC_FormClosing);
ANCform.Show();
}
private void ANC_FormClosing(object sender, FormClosingEventArgs e)
{
this.Refresh();
}

Rebind the datasource of your DataGridView in ANC_FormClosing
For example, if I were rebinding using a method that fetches data, I might write
private void ANC_FormClosing(object sender, FormClosingEventArgs e)
{
DataGridView.DataSource = GetFromDB();
}
This refreshes the grid with new data fresh from the DB

Related

C# Permanently Add and Save item to combobox

everybody!
I have collection of items in combobox's properties. And I want to add new item in my combobox by writing text in combobox and then use button event:
private void button2_Click_1(object sender, EventArgs e)
{
cbx_unix_dir.Items.Add(cbx_unix_dir.Text);
}
But in next time of starting my programm - my added item doesn't exist in combobox. What do I wrong? I need all added items have been saved in my combobox for ever. May be problem in method InitializeComponents()? May be I have to add event before it?
Thank you very much.
ComboBox has no functionality to save and reload items.
You may store items into .NET Settings file on closing window and reload them on loading form:
private void Form1_Load(object sender, EventArgs e)
{
if (Settings.Default.cboCollection != null)
this.cbx_unix_dir.Items.AddRange(Settings.Default.cboCollection.ToArray());
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ArrayList arraylist = new ArrayList(this.cbx_unix_dir.Items);
Settings.Default.cboCollection = arraylist;
Settings.Default.Save();
}
//A button to add items to the ComboBox
private void button2_Click_1(object sender, EventArgs e)
{
cbx_unix_dir.Items.Add(cbx_unix_dir.Text);
}

Refreshing DataGridView after Backgroundworker?

I have a DataGridView in a form that uses a Backgroundworker to load the data and a pleasewait form to keep the UI responsive and show a gif so the user can see the item is still working.
After it is complete I need to somehow refresh the DataGridView with the new data. If i manually click on the headers to sort my ASC or DESC then it shows the up to date data. What or how is the best way to get this grid to refresh please?
public void btnSearch_Click(object sender, EventArgs e)
{
pleaseWait.Show();
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.TestTableTableAdapter.Fill(this.TestTableData.TestTableTable, txtHotName.Text, ((System.DateTime)(System.Convert.ChangeType(txtDepartFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtDepartTo.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime)))));
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pleaseWait.Hide();
}
The above does what it needs to which is great. I just need to some how refresh without having to make the end user re-order a column to actually show refreshed data.
Following on from jdweng's comment of ;
The re-paint method doesn't automatically get called when adding data
to a DGV. So the trick is to set the datagridview1.DataSource = null,
and then back to the actual data source.
This fixed the option for me. My gif still spins and the UI updates as my criteria has changed.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pleaseWait.Hide();
this.TestTableTableDataGridView.DataSource = null;
this.TestTableTableDataGridView.DataSource = TestTableTableBindingSource;
}

DevExpress CustomRowFilter - how to trigger?

I'm very new to C# programming, so am hoping this is something simple that I'm overlooking.
I have a XtraGrid.GridControl on my form which I want to apply a CustomRowFilter against.
I am not explicitly defining any views for the grid, so I thought I'd be adding my CustomRowFilter method to an event handler against the MainView on my GridControl - however I can't find how to access the event I'm after?
Is this the right approach or am going about this the wrong way?
private void gridControl1_CustomRowFilter(object sender, RowFilterEventArgs e)
{
GridView view = sender as GridView;
DataView dv = view.DataSource as DataView;
if (1==1) //Temp - this should hide everything
{
e.Visible = false;
e.Handled = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Bind the datasource etc...
gridControl1.MainView.CustomRowFilter += gridControl1_CustomRowFilter //"Base view does not contain a definition for "CustomRowFilter"
}
The CustomRowFilter event belongs to the GridView and not to GridControl, You can access the event with :
private void Form1_Load(object sender, EventArgs e)
{
(gridControl1.MainView as GridView).CustomRowFilter += gridControl1_CustomRowFilter;
}

Refresh DataGridView using BindingSource and TableAdapter

i actually work on a Customer-DataGrid but i stuck on the Sources because i dont use really often C#.
I have a DataGridView (dataGridView1), a internal Database (Database.mdf), a BindingSource (customerBindingSource) and customerTableAdapter
Now i trying to refresh the DataSource when i click a button.
Here is a simple snippet:
private void Kundenverwaltung_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'kundenAnsicht.customer' table. You can move, or remove it, as needed.
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}
// I tried already some methods but i dont find a properly, functionally way
private void button2_Click(object sender, EventArgs e)
{
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}
I hope you can understand my problem.
~ Dennis
You need to connect your DataGridView with "customerBindingSource":
`private void Kundenverwaltung_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = this.customerBindingSource;
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}
private void button2_Click(object sender, EventArgs e)
{
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}`
It all.

Change datasource on button click in Winforms app

I have a datagridview (dataGridView1) pulling information out of a mdb datasource called rentBindingSource, how can I pull information out of hireBindingSource on a button click i.e.
private void viewHire_Click(object sender, EventArgs e)
{
// refresh datagrid view to load stuff from hireBindingSource
}
Try this:
private void viewHire_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = hireBindingSource;
}

Categories

Resources