0

我已经使用论坛上的其他答案尝试了一切。我只是希望我的数据网格视图在进行更改后选择表单上的更新按钮时动态更新。

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

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

1 回答 1

0

好的,所以我从微软找到了一个相当新的例子,它解决了我的查询。官方指南可以在这里找到:

我从 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);
    }
于 2019-04-12T16:27:43.397 回答