How to Refresh C# Datagridview After Update

C# refresh DataGridView when updating or inserted on another form


// Form A
public void loaddata()
{
//do what you do in load data in order to update data in datagrid
}

then on Form B define:

// Form B
FormA obj = (FormA)Application.OpenForms["FormA"];

private void button1_Click(object sender, EventArgs e)
{
obj.loaddata();
datagridview1.Update();
datagridview1.Refresh();
}

How to refresh or show immediately in datagridview after inserting?

Use LoadPatientRecords() after a successful insertion.

Try the below code

private void btnSubmit_Click(object sender, EventArgs e)
{
if (btnSubmit.Text == "Clear")
{
btnSubmit.Text = "Submit";

txtpFirstName.Focus();
}
else
{
btnSubmit.Text = "Clear";
int result = AddPatientRecord();
if (result > 0)
{
MessageBox.Show("Insert Successful");

LoadPatientRecords();
}
else
MessageBox.Show("Insert Fail");
}
}

How to refresh the data grid view when update? Window from C#

When you call the database_connect method, pass the form reference as below with this,

private void button1_Click(object sender, EventArgs e)
{
//Get textfield data....

//If user doesn't input the item data, show the error message. Else, update to database.
if (error == true)
{
error_msg_form emf = new error_msg_form();
emf.Show();
}
else
{
//Form1 f1 = new Form1();
database_function df = new database_function();
df.database_connect(this, item_code_tb.Text, des_tb.Text, unit_tb.Text, Convert.ToDouble(unit_price_tb.Text));
//f1.refresh_dataGridView();
}
}

Then, add the parameter Form1 form1 in the method as below and call form1.refresh_dataGridView() using the same reference.

public void database_connect(Form1 form1, String item_code, String des, String unit, double price)
{
//Form1 f1 = new Form1();
try
{

connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "INSERT into item (item_code, description, unit, price) values ('" + item_code + "', '" + des + "', '" + unit + "', " + price + ")";
command.ExecuteNonQuery();

connect.Close();
}
catch(Exception e)
{
Debug.WriteLine(e.Source);
connect.Close();
}
form1.refresh_dataGridView();
}

Also, change the code as below to refresh the data grid view,

public void refresh_dataGridView()
{
dataGridView1.DataSource = typeof(List);
dataGridView1.DataSource = df.get_view();
Debug.WriteLine("refuesh");
}

Refresh datagridview after update data in another form

Form 2 Button Click use this Code

private void button1_Click(object sender, EventArgs e)
{
Form frm=(Form)this.MdiParent;
DataGridView dt = (DataGridView)frm.Controls["dataGridView1"];
dt.Rows[0].Cells[0].Value = textBox1.Text;

}

Unable to refresh DataGridView after updating database

Following code worked write for me, first you should call the GetChanges() method on your data-set, then call the Fill(dataset.dataTable) on your dta-adapter.

            secaloFormulaDataSet.GetChanges();
materialPriceTableAdapter.Fill(secaloFormulaDataSet.materialPrice);

this will immediately updates your datagridview and other controls that use data binding in your form.

Refresh button - Refreshing data grid view after inserting, deleting, updating

The easiest way to handle this is to use a Binding Source object.

If your loading information into your DataGridView from an Access Database then your most likely storing the Data in a Dataset or DataTable.

Create a Binding Source object, and once you have populated your DataTable/Dataset, set the datasource for your Binding Source to your DataTable. Then set the Datasource from the DataGridView as the Binding Source object.

Doing this ensures that any changes in your datagridview or reflected in the DataTable and vice Versa. If you reload data into your DataTable it will reflect in the Data Grid Automatically.

DataTable dt = new DataTable();

BindingSource bs = new BindingSource();

bs.DataSource = dt;

dataGridView1.DataSource= bs;

All changes will now happen automatically.

refresh datagridview after insert or update or delete without selecting new sql query

You can do it in couple of ways.

  1. Add the newly inserted record to your datatable (you need to use a global datatable variable for this) and refresh your Grid View using this datatable.
  2. You can add the newly inserted record directly to the Grid View

You can follow these techniques also for DELETE and UPDATE

Here is the implementation of idea #1 for your existing code :

DataTable dtclubroom = new DataTable();

private void button4_Click(object sender, EventArgs e)
{
employee();
}

public void employee()
{
SqlCommand command = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, myConnection);
try
{
myConnection.Open();
dtclubroom.Clear();
command.Connection = myConnection;
command.CommandText = "Select * from employee ";
adapter.SelectCommand = command;
adapter.Fill(dtclubroom);
dataGridView2.DataSource = dtclubroom;
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
finally
{
myConnection.Close();
}
}

private void button5_Click(object sender, EventArgs e)
{
SqlCommand command2 = new SqlCommand();
try
{
myConnection.Open();
command2.CommandText = "insert into employee (name,id) values (@name,@id)";
command2.Connection = myConnection;
command2.Parameters.AddWithValue("@name","Leon");
command2.Parameters.AddWithValue("@id", "002");
command2.ExecuteNonQuery();

DataRow dr = dtclubroom.NewRow();
dr["name"] = "Leon";
dr["id"] = "002";
dtclubroom.Rows.Add(dr);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
finally
{
myConnection.Close();
}

dataGridView2.DataSource = dtclubroom; //<- refresh datagridview
}

Look that the datatable declaration is moved up and you need to place it in top of your class :

DataTable dtclubroom = new DataTable(); 

Nothing else need to be global.



Related Topics



Leave a reply



Submit