我点击插入按钮时刷新datagridview

时间:2017-02-20 05:29:37

标签: datagridview refresh

我已经搜索了这个特定问题的答案太久了,但我得不到明确的帮助。

您可以帮我刷新数据网格视图吗?当我点击插入按钮,否则我必须重新启动我的程序,以查看最近更新的数据。 我尝试过使用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 ");

    }
}

提前感谢。

1 个答案:

答案 0 :(得分:2)

刷新页面以查看更改。

因为,页面,视图或数据网格没有使用表中的新数据自动刷新或重新加载,一旦你在代码中做了一些事情!

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

    }
}