简体   繁体   中英

Refresh datagridview after insert on the same form

I have a form with a datagridview and input boxes with a button to insert values. I would like the datagridview to refresh once the button is clicked. I have tried the datagridview.refresh() and datagridview.update options. The data only appears once the application has been closed and reopened.

Here is my code:

class cFunction
{
    public static void DoSQL(string Query)
    {
        SqlConnection Connection = new SqlConnection(@" Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Zahida\Desktop\RapidsoftSupport\RapidsoftSupport\MainData.mdf;Integrated Security=True");

        SqlCommand Command = new SqlCommand(Query, Connection);
        Command.Connection.Open();
        Command.ExecuteNonQuery();
        Command.Connection.Close();
        Connection.Close();

    }
}

private void btnAdd_Click(object sender, EventArgs e)
    {
        cFunction.DoSQL("INSERT INTO Problem(SYSTEM_ID,SUBJECT, KEYWORDS) VALUES('" + SID + "','" + txtSubject.Text + "','" + txtKeywords.Text + "')");
        this.problemTableAdapter.Fill(this.mainDataDataSet1.Problem);

    }

Any help?

The grid control must bind to active and alive data source, you push some records to dataset when your program load and grid cache theme to show.

You can use binding or run select query after every insert occurred to update dataset with new records .

Zahida Kazi,

At first, you don't need to push two closure:
Command.Connection.Close();
Connection.Close();

I didn't see a gridview bindings. If you are not set a bindings just now, I can suggest you to use DataTable to create Data and bind by DataGridView.DataSource = DataTable . When data updated, you can use two ways to update gridview's data

  1. you may want to update proper grid column value
  2. you may re-bind datasource of gridview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM