刷新DataGridView,每当我点击插入按钮时



我已经为这个特定问题搜索了这个特定问题的答案,但我没有明确的帮助。

您可以帮助我刷新DataGrid视图。每当我单击插入按钮时,我都必须重新启动程序才能查看最近更新的数据。我尝试使用datagridview.refresh();和dataGridView.update();在DataGridView和插入按钮代码中,但它不起作用。这是我用于在数据库中插入数据的代码:

private void btnSave_Click(object sender, EventArgs e)
{
    DatabaseConnection dbcon = new DatabaseConnection();
    dbcon.OpenDbConnection();
    dbcon.commandConnection();
    gender1();
    string query = "Insert into Employee_personal_info (Last_Name,Middle_Name,First_Name,Phone_Number,Email_Address,Permanent_Address,Temporary_Address,Gender,userimage) values ('" + textBoxLastName.Text + "','" + textBoxMiddleName.Text + "', '" + textBoxFirstName.Text + "','" + textBoxPhoneNumber.Text + "','" + textBoxEmailAddress.Text + "','" + textBoxPermanentAdd.Text + "','" + textBoxTemporaryAdd.Text + "','" + gender1() + "','"+textBox_imagepath.Text+"') ";
    string query2 = "Select @@Identity";
    int ID;
    try
    {
        dbcon.commandExecuteNonQuery(query);
        ID = (int)dbcon.commandExecuteScalar(query2);
        string query1 = " Insert into Employee_company_info (E_ID,Department, Post, Duty_shift, Date_Hired, Date_Released) values ("+ID+",'" + comboBoxDepartment.Text + "','" + comboBoxPost.Text + "','" + comboBoxDutyShift.Text + "','" + dateTimePicker1.Value + "','" + dateTimePicker2.Value + "') ";
        dbcon.commandExecuteNonQuery(query1);
        MessageBox.Show("data inserted sucessfully");

    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.Message);
        MessageBox.Show("data not inserted ");
    }
}

预先感谢。

刷新页面以查看更改。

因为,页面,视图或数据网格不会从表中使用新数据自动重新刷新或自动重新加载!

Here, you are not updating the datagrid, instead you are updating the database table directly.因此,使用datagridview.refresh();datagridview.update();将无用。

使用location.reload();刷新页面。

或您可以use ajax instead, if you don't want the page to reload!

这是更改的代码:

private void btnSave_Click(object sender, EventArgs e)
{
    DatabaseConnection dbcon = new DatabaseConnection();
    dbcon.OpenDbConnection();
    dbcon.commandConnection();
    gender1();
    string query = "Insert into Employee_personal_info (Last_Name,Middle_Name,First_Name,Phone_Number,Email_Address,Permanent_Address,Temporary_Address,Gender,userimage) values ('" + textBoxLastName.Text + "','" + textBoxMiddleName.Text + "', '" + textBoxFirstName.Text + "','" + textBoxPhoneNumber.Text + "','" + textBoxEmailAddress.Text + "','" + textBoxPermanentAdd.Text + "','" + textBoxTemporaryAdd.Text + "','" + gender1() + "','"+textBox_imagepath.Text+"') ";
    string query2 = "Select @@Identity";
    int ID;
    try
    {
        dbcon.commandExecuteNonQuery(query);
        ID = (int)dbcon.commandExecuteScalar(query2);
        string query1 = " Insert into Employee_company_info (E_ID,Department, Post, Duty_shift, Date_Hired, Date_Released) values ("+ID+",'" + comboBoxDepartment.Text + "','" + comboBoxPost.Text + "','" + comboBoxDutyShift.Text + "','" + dateTimePicker1.Value + "','" + dateTimePicker2.Value + "') ";
        dbcon.commandExecuteNonQuery(query1);
        MessageBox.Show("data inserted sucessfully");
        location.reload(); //**Here he is**
    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.Message);
        MessageBox.Show("data not inserted ");
    }
}

最新更新