Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,

how i can to refresh datagridview after insert to the database , ( in every time click on the button , the Previous record only that appear in the grid view not the Current)
C#
  private void button2_Click(object sender, EventArgs e)
        {

//insert data
            // ... last barcode
            string JSS_connetionString001 = null;
            OleDbConnection JSS_cnn001;
            OleDbCommand JSS_cmd001;
            string JSS_sql001 = null;
            OleDbDataReader JSS_reader001;
            JSS_connetionString001 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
            JSS_sql001 = "select * from employees where employee_ID=(select max(employee_ID) from employees)";
            JSS_cnn001 = new OleDbConnection(JSS_connetionString001);
            try
            {
                JSS_cnn001.Open();
                JSS_cmd001 = new OleDbCommand(JSS_sql001, JSS_cnn001);
                JSS_reader001 = JSS_cmd001.ExecuteReader();
                while (JSS_reader001.Read())
                {

                    label01.Text = (JSS_reader001.GetValue(0) + "");
                }
                JSS_reader001.Close();
                JSS_cmd001.Dispose();
                JSS_cnn001.Close();

            }
            catch (Exception ex)
            {
                
                return;
            }

           

            // check if ID is Null

            if (label01.Text == "")
            {
                label01.Text = "0000001";
            }
            else
            {
                double JSS_prductG_ID = System.Convert.ToDouble(label01.Text) + 0000001;
                label01.Text = JSS_prductG_ID.ToString("0000000");
            }

            //insert code
            string JSS_connetionString002 = null;
            OleDbConnection JSS_connection002;
            OleDbDataAdapter JSS_oledbAdapter002 = new OleDbDataAdapter();
            string JSS_sql002 = null;
            JSS_connetionString002 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
            JSS_connection002 = new OleDbConnection(JSS_connetionString002);
            JSS_sql002 = "insert into employees(employee_ID,employee_CODE,employee_NAME,employee_NATIONAL_CODE,employee_CITY,employee_STATE,employee_VILLAGE,employee_TEL1,employee_TEL2,employee_EMAIL,employee_SALARY,employee_WORK,employee_SYSTEM_Authority,employee_PASS,employee_JOIN_DATE,employee_EDIT_DATE,employee_USER) values('" + label01.Text + "','" + textbox02.Text + "','" + textbox03.Text + "','" + textbox04.Text + "','" + textbox05.Text + "','" + textbox06.Text + "','" + textbox07.Text + "','" + textbox08.Text + "','" + textbox09.Text + "','" + textbox10.Text + "','" + textbox11.Text + "','" + textbox12.Text + "','" + label13.Text + "','" + textbox14.Text + "','" + textbox15.Text + "','" + textbox16.Text + "','" + label17.Text + "')";
            try
            {

                JSS_connection002.Open();
                JSS_oledbAdapter002.InsertCommand = new OleDbCommand(JSS_sql002, JSS_connection002);
                JSS_oledbAdapter002.InsertCommand.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
            
                return;
            }

            //Refresh

            employeesDataGridView.Refresh();
           

            
            string JSS_connetionString011 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
            string JSS_sql011 = "select * from employees";
            OleDbConnection JSS_connection011 = new OleDbConnection(JSS_connetionString011);
            OleDbDataAdapter JSS_dataadapter011 = new OleDbDataAdapter(JSS_sql011, JSS_connection011);
            DataSet JSS_ds011 = new DataSet();
            JSS_ds011.Clear();
            try
           {
                 
                 JSS_connection011.Open();
                 JSS_dataadapter011.Fill(JSS_ds011, "employees");
                 JSS_connection011.Close();
                 employeesDataGridView.DataSource = JSS_ds011;
                 employeesDataGridView.DataMember = "employees";
               
            }
            catch (Exception ex)
            {
                 return;
            }
            
            employeesDataGridView.Refresh();

}



this code don't work


but by used two button is work will

C#
  private void button2_Click(object sender, EventArgs e)
        {
 
 //insert data
            // ... last barcode
            string JSS_connetionString001 = null;
            OleDbConnection JSS_cnn001;
            OleDbCommand JSS_cmd001;
            string JSS_sql001 = null;
            OleDbDataReader JSS_reader001;
            JSS_connetionString001 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
            JSS_sql001 = "select * from employees where employee_ID=(select max(employee_ID) from employees)";
            JSS_cnn001 = new OleDbConnection(JSS_connetionString001);
            try
            {
                JSS_cnn001.Open();
                JSS_cmd001 = new OleDbCommand(JSS_sql001, JSS_cnn001);
                JSS_reader001 = JSS_cmd001.ExecuteReader();
                while (JSS_reader001.Read())
                {

                    label01.Text = (JSS_reader001.GetValue(0) + "");
                }
                JSS_reader001.Close();
                JSS_cmd001.Dispose();
                JSS_cnn001.Close();

            }
            catch (Exception ex)
            {
                
                return;
            }

           

            // check if ID is Null

            if (label01.Text == "")
            {
                label01.Text = "0000001";
            }
            else
            {
                double JSS_prductG_ID = System.Convert.ToDouble(label01.Text) + 0000001;
                label01.Text = JSS_prductG_ID.ToString("0000000");
            }

            //insert code
            string JSS_connetionString002 = null;
            OleDbConnection JSS_connection002;
            OleDbDataAdapter JSS_oledbAdapter002 = new OleDbDataAdapter();
            string JSS_sql002 = null;
            JSS_connetionString002 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
            JSS_connection002 = new OleDbConnection(JSS_connetionString002);
            JSS_sql002 = "insert into employees(employee_ID,employee_CODE,employee_NAME,employee_NATIONAL_CODE,employee_CITY,employee_STATE,employee_VILLAGE,employee_TEL1,employee_TEL2,employee_EMAIL,employee_SALARY,employee_WORK,employee_SYSTEM_Authority,employee_PASS,employee_JOIN_DATE,employee_EDIT_DATE,employee_USER) values('" + label01.Text + "','" + textbox02.Text + "','" + textbox03.Text + "','" + textbox04.Text + "','" + textbox05.Text + "','" + textbox06.Text + "','" + textbox07.Text + "','" + textbox08.Text + "','" + textbox09.Text + "','" + textbox10.Text + "','" + textbox11.Text + "','" + textbox12.Text + "','" + label13.Text + "','" + textbox14.Text + "','" + textbox15.Text + "','" + textbox16.Text + "','" + label17.Text + "')";
            try
            {

                JSS_connection002.Open();
                JSS_oledbAdapter002.InsertCommand = new OleDbCommand(JSS_sql002, JSS_connection002);
                JSS_oledbAdapter002.InsertCommand.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
            
                return;
            }
}


 private void button3_Click(object sender, EventArgs e)
        {
            //Refresh

            employeesDataGridView.Refresh();
           

            
            string JSS_connetionString011 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
            string JSS_sql011 = "select * from employees";
            OleDbConnection JSS_connection011 = new OleDbConnection(JSS_connetionString011);
            OleDbDataAdapter JSS_dataadapter011 = new OleDbDataAdapter(JSS_sql011, JSS_connection011);
            DataSet JSS_ds011 = new DataSet();
            JSS_ds011.Clear();
            try
           {
                 
                 JSS_connection011.Open();
                 JSS_dataadapter011.Fill(JSS_ds011, "employees");
                 JSS_connection011.Close();
                 employeesDataGridView.DataSource = JSS_ds011;
                 employeesDataGridView.DataMember = "employees";
               
            }
            catch (Exception ex)
            {
                 return;
            }
            
            employeesDataGridView.Refresh();
            
            }



other detials


the first code make refresh to data grid veiw, but after pressing the button twice and don't show the current input but it works well if it is put in a separate button Event,and the table flashed (show the new value and disappear quickly and does not show except in the next time)
Posted
Updated 6-Mar-14 4:12am
v3

After Saving Bind data to gridView...employeesDataGridview.DataBind();
 
Share this answer
 
v2
Comments
Golden Basim 2-Mar-14 23:38pm    
thanks for reply

Error System.Windows.Forms.DataGridView' does not contain a definition for 'DataBind' and no extension method 'DataBind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found
k07 3-Mar-14 0:36am    
Gridview .dataBind() is working in Webapplication.......try this FormName.Refresh();........I don't no it is working or not....
Golden Basim 3-Mar-14 2:37am    
no change
All you have to do is rebind the datagridview after any action. Check this answer(which is by enquirer)
how to refresh datagridview in vb.net[^]
 
Share this answer
 
Comments
Golden Basim 3-Mar-14 14:23pm    
this.employeesDataGridView.EndEdit();
this.employeesDataGridView.Refresh();
this.employeesDataGridView.Parent.Refresh();

( don't work )





employeesDataGridView.DataBindings.Clear();


this.employeesTableAdapter.Fill(this.je_salestoredbDataSet.employees);
employeesDataGridView.Update();
employeesDataGridView.Refresh();

( don't work )
Golden Basim 4-Mar-14 17:07pm    
please help ,
( in every time click on the button , the Previous record only that appear in the grid view not the Current)
thatraja 5-Mar-14 1:54am    
Did you check the link in my answer? You should re-assign the datasource to refresh gridview like below
DataGridView1.DataSource = ds.Tables(0);
Golden Basim 5-Mar-14 8:24am    
thanks for reply:
by add this line employeesDataGridView.DataSource = JSS_ds011.Tables(0);
error :
Non-invocable member 'System.Data.DataSet.Tables' cannot be used like a method.
thatraja 5-Mar-14 8:34am    
My mistake, there should be square brackets
Try employeesDataGridView.DataSource = JSS_ds011.Tables[0];
Data won't be refreshed this way.
After adding new record, updating an existing record or say deleting a record, you must bind the data again to the gridview in order to reflect the changes.
-KR
 
Share this answer
 
Comments
Golden Basim 6-Mar-14 12:19pm    
thanks for reply
i used all this answer but don't work ,

1-
http://stackoverflow.com/questions/1449773/c-sharp-windows-forms-basic-data-grid


2-
string JSS_connetionString011 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
string JSS_sql011 = "select * from employees";
OleDbConnection JSS_connection011 = new OleDbConnection(JSS_connetionString011);
OleDbDataAdapter JSS_dataadapter011 = new OleDbDataAdapter(JSS_sql011, JSS_connection011);
DataSet JSS_ds011 = new DataSet();
JSS_dataadapter011.Fill(JSS_ds011, "employees");
employeesDataGridView.DataSource = JSS_ds011.Tables["employees"];




3-
//Refresh
this.employeesTableAdapter.Fill(this.je_salestoredb.employees);
employeesDataGridView.Refresh();


how i can to bind the data again to the gridview
Krunal Rohit 6-Mar-14 12:23pm    
Okay, create one method that selects data from database i.e SELECT * FROM SomeTable and stuff.
Now let's say, you've entered one record and you want to see immediately after entering it. So what you can do is, as soon as the Insert operation is done , call that created method that selects data.

-KR
Golden Basim 6-Mar-14 12:40pm    
i write this code :






private void button1_Click(object sender, EventArgs e)
{
//insert code
/*

*/
GetData("");
}



static void GetData(OleDbConnection connection)
{
string JSS_connetionString011 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
DataSet JSS_ds011 = new DataSet();
DataTable dt = new DataTable();
string JSS_sql011 = "select * from employees";
OleDbConnection JSS_connection011 = new OleDbConnection(JSS_connetionString011);
OleDbDataAdapter JSS_dataadapter011 = new OleDbDataAdapter(JSS_sql011, JSS_connection011);
JSS_dataadapter011.SelectCommand = new OleDbCommand(JSS_sql011, JSS_connection011);
JSS_ds011.Clear();
try
{
JSS_Admin_Employees frm = new JSS_Admin_Employees();

JSS_connection011.Open();
JSS_dataadapter011.Fill(JSS_ds011, "employees");
JSS_connection011.Close();
frm.employeesDataGridView.DataSource = JSS_ds011;
frm.employeesDataGridView.DataMember = "employees";
frm.employeesDataGridView.DataSource = JSS_ds011.Tables[0];



}
catch (Exception ex)
{

return;
}

}





result :
no thing happen for Data Grid View but , but when i moved the Scroll bar the new value appeared Instead of first record.
Krunal Rohit 6-Mar-14 12:42pm    
okay, that means that you're getting new values here, right ?
So do one thing, in this method, make the data of gridview to null at very first and then select the data.

-KR
Krunal Rohit 6-Mar-14 12:44pm    
And you have used two DataSource. I don't think first one is necessary. So remove either of them from code.

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900