简体   繁体   中英

Unable to refresh DataGridView that is bound to a DataTable

I've tried everything using other answers on the forum. I simply want my data grid view to dynamically update when i select the update button on my form after making a change.

Ref the code below, the current result is that when I add a new row and press the update button, the data grid view simply appends all existing records (and the new row) underneath so the list keeps growing in size with duplicate values.

 public UserGroupsGridViewForm()
    {
        InitializeComponent();
    }

    private void UserGroupsGridViewForm_Load(object sender, EventArgs e)
    {
        LoadUserGroupsToDataTable();
    }

    public static SqlCommandBuilder userGroupsSqlCommandBuilder;
    public static DataTable userGroupsDataTable = new DataTable();
    public static SqlDataAdapter userGroupsSqlAdaptor;

    public void LoadUserGroupsToDataTable()
    {
        try
        {
            SqlConnection connection = new SqlConnection(connectionString);
            string cmdText1 = "SELECT * FROM [dbo].[UserGroups]";
            userGroupsSqlAdaptor = new SqlDataAdapter(cmdText1, connection);
            userGroupsSqlCommandBuilder = new SqlCommandBuilder(userGroupsSqlAdaptor);
            userGroupsSqlAdaptor.Fill(userGroupsDataTable);
        }
        catch (Exception ex)
        {
            log.Error(ex);
            SystemEvents.DatabaseExceptions(ex);
        }
        LoadDataTabletoGridView();
    }

    private void LoadDataTabletoGridView()
    {
        try
        {
            UserGroupsGridView1.DataSource = userGroupsDataTable;
        }
        catch (Exception ex)
        {
            SystemEvents.DatabaseExceptions(ex);
        }
    }

    private void SaveChangesButton_Click(object sender, EventArgs e)
    {
        userGroupsSqlAdaptor.Update(userGroupsDataTable);
        //UserGroupsGridView1.Update(); // not working!
        //UserGroupsGridView1.Refresh(); // not working!
        LoadUserGroupsToDataTable();
    }

OK, so i found a fairly new example from Microsoft whuich solved my query. The official guide can be found here:

The only real change I made from the Microsoft example was to combine the two methods for update and reload (using a single Save button) on my form, so the changes would be reflected immediately.

 private void UserGroupsGridViewForm_Load(object sender, EventArgs e)
    {
        LoadDataTabletoGridView();
    }

    private readonly BindingSource bindingSource1 = new BindingSource();
    private SqlDataAdapter dataAdapter = new SqlDataAdapter();

    public void GetData(string selectCommand)
    {
        try
        {
            SqlConnection connection = new SqlConnection(connectionString);
            //string cmdText1 = "SELECT * FROM [dbo].[UserGroups]";

            // Create a new data adapter based on the specified query.
            dataAdapter = new SqlDataAdapter(selectCommand, connection);

            // Create a command builder to generate SQL update, insert, and
            // delete commands based on selectCommand. 
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

            // Populate a new data table and bind it to the BindingSource.
            DataTable table = new DataTable
            {
                Locale = CultureInfo.InvariantCulture
            };
            dataAdapter.Fill(table);
            bindingSource1.DataSource = table;
        }
        catch (Exception ex)
        {
            log.Error(ex);
            SystemEvents.DatabaseExceptions(ex);
        }
    }

    private void LoadDataTabletoGridView()
    {
        // Bind the DataGridView to the BindingSource
        // and load the data from the database.
        UserGroupsGridView.DataSource = bindingSource1;
        GetData("SELECT * FROM [dbo].[UserGroups]");
    }

    private void SaveChangesButton_Click(object sender, EventArgs e)
    {
        // Update the database with changes.
        dataAdapter.Update((DataTable)bindingSource1.DataSource);

        // Reload the data from the database.
        GetData(dataAdapter.SelectCommand.CommandText);
    }

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