RobertA
RobertA

Reputation: 1

Refresh DataGridView after INSERT into SQL

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

Upvotes: 0

Views: 1294

Answers (3)

Greko2015 GuFn
Greko2015 GuFn

Reputation: 589

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

Upvotes: 1

RobertA
RobertA

Reputation: 1

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

Upvotes: 0

Lekve
Lekve

Reputation: 168

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

Upvotes: 0

Related Questions