【问题标题】:Unable to refresh DataGridView that is bound to a DataTable无法刷新绑定到 DataTable 的 DataGridView
【发布时间】:2019-04-11 22:58:50
【问题描述】:

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

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

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

【问题讨论】:

    标签: datagridview sqlcommandbuilder


    【解决方案1】:

    好的,所以我从 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);
        }
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 2015-11-29
    • 2013-09-13
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多