How do I Update/Reload DataGridView BindingSource?

前端 未结 2 1714
無奈伤痛
無奈伤痛 2021-01-24 18:08

I am new to C#, Windows Forms, and datagridviews. I have a tabbed form: tab 1 displays a datagridview of the exercises table; tab 2 is for adding a new exercise to the table. Th

相关标签:
2条回答
  • 2021-01-24 18:23

    You have to reload your Dataset with the information from the database after doing the insert. It's not automatic!

    0 讨论(0)
  • 2021-01-24 18:35

    Create a LoadDataGridView method:

    private void LoadDataGridView() {
        // Fill a DataAdapter using the SelectCommand.
        DataAdapter da = null;
    
        // The Sql code here
    
        // In case something fails, bail out of the method.
        if (da == null) return;
    
        // Clear the DataSource or else you'll get double of everything.
        if (exerciseListDataGridView.DataSource != null) {
            exerciseListDataGridView.DataSource.Clear();
            exerciseListDataGridView.DataSource = null;
        }
    
        // I'm doing this off the top of my head, you may need to fill a DataSet here.
        exerciseListDataGridView.DataSource = da.DefaultView;
    
    }
    

    Now all you have to do is call that method in both your Form1_Load() and at the end of your InsertExcercise(). If you have to use a DataSet, don't forget to dispose of the DataAdapter object at the end to conserve resources.

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