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;
}
Related
I recently started to learn C# and right now I want to mess arouwnd with the Form[Design].
Right now I'm trying to BringToFront() custom controls each time I hover over a button (Ive got a few buttons close to each other, each time I hover over them I get a certain User Control).
This is what I've got so far:
private void button1_Hover(object sender, EventArgs e)
{
costumControl1.BringToFront();
}
private void button1_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button2_Hover(object sender, EventArgs e)
{
costumControl2.BringToFront();
}
private void button2_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button3_Hover(object sender, EventArgs e)
{
costumControl3.BringToFront();
}
private void button3_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
But bringing to front the user control costumControl0 when I hover from a button to another it's not what I want.
But I don't know how to go about this.
Is it possible to just add a if statement where I check if I'm not hovering the buttons close to my current one and then display the costumControl0.
Or a timer is necessary to delay the display of the costumControl0 and skip the command if I'm starting another event.
If the timer is needed, can I use one timer for all of the buttons or do I need to create one for each?
Or whats the best approach for this?
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
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.
I have a grid view that has a column called Amount .I have a textbox that shows the sum of amount column in gridview ,but i need to be aware when items in gridview are inserted changed and deleted because i need to update the textbox
Has anyone handled such a scenario. I would like to know which datagrid event I should be using,
I am using windows form application
Best regards
You can make use of RowsRemoved, RowsAdded and CellValueChanged events.
Updated
You can use these assuming your user is only adding and deleting 1 row at a time. also added a method with a linq statement to sum up your amount and assign it to the textbox.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.Equals(dataGridView1.Columns["Amount"].Index))
{
UpdateTotal();
}
}
private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
UpdateTotal();
}
private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
UpdateTotal();
}
private void UpdateTotal()
{
textBox1.Text = (dataGridView1.Rows.Cast<DataGridViewRow>()
.Sum(t => Convert.ToInt32(t.Cells["Amount"].Value))).ToString();
}
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;
}