0

I got my form "InsertClient" and I have the button click method

        public void Insert_Button_Click(object sender, EventArgs e)
    {
        MyClass.InsertNewClient(fullNametxt.Text, shortNametxt.Text);
        fullNametxt.Clear();
        shortNametxt.Clear();
        fullNametxt.Focus();
        GridView.Update();
        GridView.Refresh();
    }

My class recieve this:

    public static void InsertNewClient (String fullName, String shortName)
    {
        SqlConnection conn = DBClass.ConnectionString.GetConnection();
        SqlCommand cmd = new SqlCommand("MyStoredProcedure", conn);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@fullName", fullName);
        cmd.Parameters.AddWithValue("@shortName", shortName);
        int i = cmd.ExecuteNonQuery();
        if (i == 0)
        {
            MessageBox.Show("Can't save data");
        }
        if (i > 0)
        {
            MessageBox.Show("Data saved!");
        }
    }

The thing is, the data is saved but that the DataGridView does not refresh after each INSERT (button click). I have to close the form and re-open it and appears refreshed.

Please help, I want the DataGridView refresh after INSERT data

RobertA
  • 1
  • 3
  • possible duplicate of [How to refresh or show immediately in datagridview after inserting?](http://stackoverflow.com/questions/21299016/how-to-refresh-or-show-immediately-in-datagridview-after-inserting) – mmking Apr 16 '15 at 19:24
  • datagridview binding source =? – محمد النعيمي Apr 16 '15 at 19:39
  • Show how you loading data to the `datagridview` – Fabio Apr 16 '15 at 21:03
  • @RobertA if you want to post the code that solved your problem, you should post it as an answer, not a comment. Then if someone read this question and wanted to know how you solved it, they can just read it below instead of digging through the comments for it. – mmking Apr 16 '15 at 21:49

3 Answers3

1

cCUSTOMERBindingSource Is the object of my BindingSource generated using ToolBox.

public void ReloadGrid()
    {
        Cursor.Current = Cursors.WaitCursor;
        cCUSTOMERBindingSource.DataSource = bd.C_CUSTOMER.ToList();
        Cursor.Current = Cursors.Default;
    }

this where I called the method

private void bunifuThinButton21_Click(object sender, EventArgs e)
    {
        C_CUSTOMER cst = new C_CUSTOMER();
        C_ACCOUNT acc = new C_ACCOUNT();

        cst.FIRST_NAME = txtFname.Text;
        cst.MIDDLE_NAME = txtMname.Text;
        cst.LAST_NAME = txtLname.Text;
        cst.LOCATION = txtlocation.Text;
        cst.DATE_OF_BIRTH = Convert.ToDateTime(dateTimePicker1.Text);

        acc.ACCOUNT_BALANCE = Convert.ToInt32(txtInitAmoun.Text);
        acc.ACCOUNT_NUMBER = Convert.ToInt32(txtAccNumber.Text);
        cst.TELEPHONE = Convert.ToInt32(txtTelephone.Text);

        cst.DATE_CREATE = DateTime.Now;

        int newID = cstDao.insertCustomer(cst.FIRST_NAME, cst.MIDDLE_NAME, cst.LAST_NAME, cst.LOCATION, cst.DATE_OF_BIRTH, cst.TELEPHONE, cst.MODIFY_BY, cst.DATE_MODIFY, cst.DATE_CREATE);
        acc.CUSTOMER_ID = newID;
        acc.DATE_CREATED = DateTime.Now;
        acc.CREATED_BY = 1;
        int newAccID  = cstDao.insertAccount(acc);

        if(newID != 0 && newAccID != 0) { 
        MessageBox.Show("Insert Succefull", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        else
        {
            MessageBox.Show("Error during the insertion", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        ReloadGrid();

    }

On Form load

private void Form3_Load(object sender, EventArgs e)
        {
            bd = new Customer_DBEntities();
            cCUSTOMERBindingSource.DataSource = bd.C_CUSTOMER.ToList();
        }
Greko2015 GuFn
  • 512
  • 6
  • 13
0

You should have separate method for example Refresh() which in your case will obtain data from sql using sqlDataReader and after your insertBtn call that method and initialize dataGridView DataSource to it

Lekve
  • 168
  • 3
  • 11
0

Something like that @mmking, but not exactly I already solved it.. the problem was exactly what @Fabio said...

Inside of the button click method I fill again de DataGridView with the DataSet.

public void Insert_Button_Click(object sender, EventArgs e)
{
    MyClass.InsertNewClient(fullNametxt.Text, shortNametxt.Text);
    this.tblClientTableAdapter.Fill(this.DataSetClient.tblClient);
}

Just to get clear... I use the DataGridView of toolbox, select "Choose data source" and select the table that I want to use to fill DataGrid...

I use the DataSetName that gave me by default and put it right like I show it in the code

Hope it helps for future questions

RobertA
  • 1
  • 3