How to refresh or show immediately in datagridview after inserting?

后端 未结 8 1054
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 14:32

After entering data into all the textbox, and after clicking the submit button, it won\'t immediately show in the datagridview, I need to reopen the form in order to see the

相关标签:
8条回答
  • 2020-11-28 14:47

    Use LoadPatientRecords() after a successful insertion.

    Try the below code

    private void btnSubmit_Click(object sender, EventArgs e)
    {
            if (btnSubmit.Text == "Clear")
            {
                btnSubmit.Text = "Submit";
    
                txtpFirstName.Focus();
            }
            else
            {
               btnSubmit.Text = "Clear";
               int result = AddPatientRecord();
               if (result > 0)
               {
                   MessageBox.Show("Insert Successful");
    
                   LoadPatientRecords();
               }
               else
                   MessageBox.Show("Insert Fail");
             }
    }
    
    0 讨论(0)
  • 2020-11-28 14:51

    Try refreshing the datagrid after each insert

    datagridview1.update();
    datagridview1.refresh();  
    

    Hope this helps you!

    0 讨论(0)
  • 2020-11-28 14:52

    You can set the datagridview DataSource to null and rebind it again.

    private void button1_Click(object sender, EventArgs e)
    {
        myAccesscon.ConnectionString = connectionString;
    
        dataGridView.DataSource = null;
        dataGridView.Update();
        dataGridView.Refresh();
        OleDbCommand cmd = new OleDbCommand(sql, myAccesscon);
        myAccesscon.Open();
        cmd.CommandType = CommandType.Text;
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataTable bookings = new DataTable();
        da.Fill(bookings);
        dataGridView.DataSource = bookings;
        myAccesscon.Close();
    }
    
    0 讨论(0)
  • 2020-11-28 14:57

    Only need to fill datagrid again like this:

    this.XXXTableAdapter.Fill(this.DataSet.XXX);
    

    If you use automaticlly connect from dataGridView this code create automaticlly in Form_Load()

    0 讨论(0)
  • 2020-11-28 14:59

    In the form designer add a new timer using the toolbox. In properties set "Enabled" equal to "True".

    The set the DataGridView to equal your new data in the timer

    0 讨论(0)
  • 2020-11-28 15:06

    I don't know if you resolved your problem, but a simple way to resolve this is rebuilding the DataSource (it is a property) of your datagridview. For example:

    grdPatient.DataSource = MethodThatReturnList();

    So, in that MethodThatReturnList() you can build a List (List is a class) with all the items you need. In my case, I have a method that return the values for two columns that I have on my datagridview.

    Pasch.

    0 讨论(0)
提交回复
热议问题