Refresh DataGridView using BindingSource and TableAdapter - c#

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.

Related

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;
}

C# - Binding FormClosing event to cause Form refresh

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

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;
}

Update Error in cell

When I edit my DataGridView's cell I want to immediately update my MSSQL database. I added a CellEndEdit' event handler. Also I added autogenerated bindingsource andTableAdapter`.
It works correctly but whenever I want to edit my value I get an error. Actually, I wrote an update method with SqlDataAdapter. But in this time I use auto-generated TableAdapter. I don't know what to do. I try this one but I got an error.
Note: DataSet11, loginTableAdapter, and Bindingsource1 are auto generated and work.
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
querybuilder.GetUpdateCommand();
loginTableAdapter.Update(dataset11);
}
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
querybuilder.GetUpdateCommand();
loginTableAdapter.Update(dataset11);
}
}
//or if you has a bindingSource
private void bindingSource1_CurrentItemChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
querybuilder.GetUpdateCommand();
loginTableAdapter.Update(dataset11);
}
}

Button1 not being clicked on startup

I am having an issue with my WinForm application.
Below I have my code for my button that I want to click.
private void button1_Click(object sender, EventArgs e)
{
// Do code.
}
Now I want to run the program on start up, so I have this code below:
private void form_Load(object sender, EventArgs e)
{
this.button1_Click(object sender, EventArgs e);
}
But this does not work. It has red lines under the words: "sender", "EventArgs e"
What am I doing wrong, Please help me?
Any help would be greatly appreciated, thanks!
First if you want to click button that way you should do:
private void form_Load(object sender, EventArgs e)
{
button1.PerformClick();
}
Second,
it is not a good idea to do it anyway, better approach is to create common method that is call in button_click event and form_load event.

Categories

Resources