无法刷新绑定到DataTable的DataGridView

时间:2019-04-11 22:58:51

标签: datagridview sqlcommandbuilder

我在论坛上尝试了其他答案。我只是希望在更改后在表单上选择更新按钮时,数据网格视图能够动态更新。

参考下面的代码,当前结果是,当我添加新行并按“更新”按钮时,数据网格视图仅将所有现有记录(和新行)附加在其下,因此列表的大小不断增加,重复项值。

 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();
    }

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了一个来自Microsoft的相当新的示例,它解决了我的查询问题。官方指南可以在这里找到:

我对Microsoft示例所做的唯一真正的更改是结合了两种更新和重新加载方法(使用单个“保存”按钮),因此更改将立即反映出来。

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