Reputation: 806
I am trying to refresh the data grid view after the user insert some item to the database. And I tried to null the data source of the view and apply a new data source to it, but it didn't work.
Here is the flow of the system:
add-item form
In this part, there will have two forms
and one database claee
. They are Form1
(the data view), Add-item form
and the database_function.cs
.
Here is my code on database class
:
public class database_function
{
OleDbConnection connect = new OleDbConnection();
public database_function()
{
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\sys_db.accdb";
}
public void database_connect(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();
}
f1.refresh_dataGridView();
}
//return the dataGridView to form 1, and show the database data on it.
public DataTable get_view()
{
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
string query = "Select item_code, description, unit, price from item";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
connect.Close();
Debug.WriteLine("return dataTable");
return dt;
}
}
Here is the code on form1
:
//Get the data source when the form1 is loading
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = df.get_view();
}
//Refresh the view
public void refresh_dataGridView()
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = df.get_view();
Debug.WriteLine("refuesh");
}
Here is the add-item. form
:
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(item_code_tb.Text, des_tb.Text, unit_tb.Text, Convert.ToDouble(unit_price_tb.Text));
//f1.refresh_dataGridView();
}
}
However, I debug log will show the refresh
, that mean it can run in the refresh_dataGridView()
. But it cannot refresh the view.
What is the problem is it? Or there have another way to do this? Thanks
Upvotes: 1
Views: 14248
Reputation: 32445
I assume you close "AddItem" form when item have been inserted to the database. My suggestion to make notification about successful insertion more clearer without passing references and exposing form's control by making it public
Change you database_connect
method to return true/false
based on result of query
public void database_connect(String item_code, String des, String unit, double price)
{
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 + ")";
int insertedRows = command.ExecuteNonQuery();
return insertedRows > 0;
}
catch(Exception e)
{
Debug.WriteLine(e.Source);
}
finally
{
connect.Close();
}
// false will be returned if exception will be thrown
return false;
}
Then in the AddItem
form set Form.DialogResult
property to Ok
if item was added successfully.
public class AddNewItemForm : Form
{
private void button1_Click(object sender, EventArgs e)
{
if (error == true)
{
error_msg_form emf = new error_msg_form();
emf.Show();
}
else
{
database_function df = new database_function();
bool isAdded = df.database_connect(item_code_tb.Text,
des_tb.Text,
unit_tb.Text,
Convert.ToDouble(unit_price_tb.Text));
if(isAdded)
{
this.DialogResult = DialogResult.Ok;
}
}
}
}
Then in the Main form use ShowDialog
method to get result about successful insertion.
public class MainForm: Form
{
private void ButtonAddItem_Click(object sender, EventArgs e)
{
using(var addItemForm = new AddNewItemForm())
{
if(addItemForm.ShowDialog() == DialogResult.Ok)
{
// here you can update your DataGridView
this.DataGridView.DataSource = df.get_view();
}
}
}
}
Upvotes: 0
Reputation: 12022
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");
}
Upvotes: 1
Reputation: 63065
You better refactor your code and separate database access code into separate class file. then you can call those functions from other classes, forms etc..
In your Form just after insert item method you can set the gridview data source, for example:
database_function df = new database_function();
df.InsertItem("sdfsdf", "Description", "KG", 5);
//load data again
dataGridView1.DataSource = df.get_view();
you need two methods, one for insert and one for get details
public class database_function
{
public void InsertItem(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();
}
//f1.refresh_dataGridView();
}
public DataTable get_view()
{
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
string query = "Select item_code, description, unit, price from item";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
connect.Close();
return dt;
}
}
Upvotes: 2
Reputation: 3576
This is the code I used for mine:
private void Form1_Activated(object sender, EventArgs e) {
if (GlobalVariables.bReload == false) {
Activated -= Form1_Activated;
GlobalVariables.bReload = true;
dgv1.DataSource = "my datasource"
dgv1.Refresh();
Activated += Form1_Activated;
}
}
In my case I wanted to trigger the update it after I made a modification on a subform and came back to the main form where my datagridview was located. I used a boolean type global variable to manage it.
Upvotes: 0