【问题标题】:refresh datagridview when ever i hit insert button当我点击插入按钮时刷新datagridview
【发布时间】:2017-02-20 05:29:36
【问题描述】:

我已经为这个特定问题寻找答案太久了,但我没有得到明确的帮助。

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

    }
}

提前致谢。

【问题讨论】:

  • 当您加载数据并将其附加到数据网格中时,您的代码在哪里?

标签: datagridview refresh


【解决方案1】:

刷新页面以查看更改。

因为,页面、视图或数据网格不会使用表中的新数据自动刷新或重新加载,一旦您在代码中执行了某些操作!

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

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    相关资源
    最近更新 更多