簡體   English   中英

插入SQL后刷新DataGridView

[英]Refresh DataGridView after INSERT into SQL

我得到了表單“ InsertClient”,並且具有按鈕單擊方法

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

我班上收到這個:

    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!");
        }
    }

事實是,已保存數據,但每次插入(單擊按鈕)后DataGridView不會刷新。 我必須關閉該窗體並重新打開它,然后看起來很刷新。

請幫忙,我想在插入數據后刷新DataGridView

cCUSTOMERBindingSource是使用ToolBox生成的BindingSource對象。

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

這就是我所說的方法

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

    }

在表格上

private void Form3_Load(object sender, EventArgs e)
        {
            bd = new Customer_DBEntities();
            cCUSTOMERBindingSource.DataSource = bd.C_CUSTOMER.ToList();
        }

您應該有單獨的方法,例如Refresh(),在這種情況下,您將使用sqlDataReader從sql獲取數據,並在insertBtn調用該方法並將dataGridView數據源初始化后使用該方法

像@mmking這樣的東西,但我還沒完全解決。.問題恰好是@Fabio所說的...

在按鈕單擊方法內部,我再次用DataSet填充了DataGridView。

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

只是要弄清楚...我使用工具箱的DataGridView,選擇“選擇數據源”,然后選擇要用於填充DataGrid的表...

我使用默認情況下給我的DataSetName並將其正確設置,就像我在代碼中顯示的那樣

希望對以后的問題有所幫助

暫無
暫無

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

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