chopperfield
chopperfield

Reputation: 567

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

I just want to clarify, can i refresh datagridview after insert or update or delete without selecting new sql query again ? i have googled it, and still have no idea to do it.

here's my code

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

    public void employee()
    {
        DataTable dtclubroom = new DataTable();
        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();
        }
        catch (Exception  exc)
        {
            MessageBox.Show(exc.Message);
        }
        finally
        {
            myConnection.Close();
        }
        employee() //<- refresh datagridview
    }

Button 4 is to load data, and button 5 inserting data also load data. is there a way refresh datagridview without calling employee() method again ?

Upvotes: 1

Views: 1478

Answers (1)

Md. Suman Kabir
Md. Suman Kabir

Reputation: 5453

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.

Upvotes: 1

Related Questions