簡體   English   中英

插入或更新或刪除后刷新datagridview而不選擇新的sql查詢

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

我只是想澄清一下,我可以在插入或更新或刪除后刷新 datagridview 而不再次選擇新的 sql 查詢嗎? 我已經用谷歌搜索了它,但仍然不知道要這樣做。

這是我的代碼

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是加載數據, button 5插入數據也加載數據。 有沒有辦法在不再次調用employee()方法的情況下刷新datagridview?

您可以通過幾種方式做到這一點。

  1. 將新插入的記錄添加到您的數據表(您需要為此使用全局數據表變量)並使用此數據表刷新您的Grid View
  2. 您可以將新插入的記錄直接添加到Grid View

您也可以按照這些技術進行DELETEUPDATE

這是您現有代碼的想法 #1 的實現:

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
}

看看數據表聲明被上移了,你需要把它放在你的班級的頂部:

DataTable dtclubroom = new DataTable(); 

沒有其他東西需要是全球性的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM