无法刷新绑定到 DataTable 的 DataGridView

     2023-05-08     135

关键词:

【中文标题】无法刷新绑定到 DataTable 的 DataGridView【英文标题】:Unable to refresh DataGridView that is bound to a DataTable 【发布时间】: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();
    

【问题讨论】:

【参考方案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);
    

【讨论】:

docs.microsoft.com/en-us/dotnet/framework/winforms/controls/…

使用 TemplateColumns 将 WPF DataGrid 绑定到 DataTable

】使用TemplateColumns将WPFDataGrid绑定到DataTable【英文标题】:BindingWPFDataGridtoDataTableusingTemplateColumns【发布时间】:2011-02-1006:29:42【问题描述】:我已经尝试了所有方法,但一无所获,所以我希望有人能给我带来欢呼的时刻。我根本... 查看详情

将行号列添加到绑定到 DataTable 的 DataGridView

】将行号列添加到绑定到DataTable的DataGridView【英文标题】:AddrownumbercolumntoDataGridViewboundtoDataTable【发布时间】:2020-03-1306:41:33【问题描述】:在ADO.NET中,我使用DataAdapter.Fill(..)调用来使用数据库中的值填充DataTable。然后我将DataTa... 查看详情

绑定到 DataTable 的 DataGridView 未显示

】绑定到DataTable的DataGridView未显示【英文标题】:DataGridViewboundtoDataTableisnotshowing【发布时间】:2015-05-0415:39:03【问题描述】:我试图在绑定到DataTable的表单中显示DataGridView,但它没有显示出来。我正在使用C1TrueDBGrid执行此操作... 查看详情

datagridview绑定list<t>不显示数据

...wuser();、数据绑定的问题:当DataGridView的DataSource绑定的为DataTable时,当DataTable的内容发生改变时,DataGridView中的内容会自动跟随DataTable改变而不用重新绑定数据源;而把List<T>绑定到DataGridView则不然,当List<T>的内容发生... 查看详情

在 DataTable 更改 WPF 后更新绑定到 DataTable 的 DataGrid

】在DataTable更改WPF后更新绑定到DataTable的DataGrid【英文标题】:UpdateDataGridboundtoDataTableafterDataTableisalteredWPF【发布时间】:2011-04-0701:21:56【问题描述】:我有一个组合框,用户可以在其中选择特定年份(由数据库填充)并查看该... 查看详情

将 DataGrid 绑定到两个 DataTable

】将DataGrid绑定到两个DataTable【英文标题】:BindingDataGridtotwoDataTable【发布时间】:2015-12-1408:31:56【问题描述】:我有两个DataTable,它们的结构、架构和约束完全相同,但行不同。我需要将这些表显示为DataGrid,并连接在一起,即... 查看详情

如何将 DataTable 绑定到 DataGrid

】如何将DataTable绑定到DataGrid【英文标题】:HowBindDataTabletoDataGrid【发布时间】:2011-10-1202:24:39【问题描述】:这是我的数据表。DataTable_simpleDataTable=newataTable();varperson=newDataColumn("Person")DataType=typeof(Person);_simpleDataTable.Columns.A 查看详情

无法将属性绑定到“FlywayProperties”,AnnotationConfigApplicationContext@5454d35e 尚未刷新

】无法将属性绑定到“FlywayProperties”,AnnotationConfigApplicationContext@5454d35e尚未刷新【英文标题】:Couldnotbindpropertiesto\'FlywayProperties\',AnnotationConfigApplicationContext@5454d35ehasnotbeenrefreshedyet【发布时间】:2021-12-1700:44:55【问题 查看详情

刷新 WPF Datagrid 未绑定到可观察集合?

...。我遇到的问题是直接在数据库上运行SQL语句后,我似乎无法刷新更新的数据。数据库数据已正确更新,关闭 查看详情

更改绑定到 DataGridView 的 DataTable 中的列顺序不会反映在视图中

】更改绑定到DataGridView的DataTable中的列顺序不会反映在视图中【英文标题】:ChangingcolumnorderinDataTableboundtoDataGridViewdoesnotreflectintheview【发布时间】:2010-10-0416:49:05【问题描述】:当应用程序运行时,DataGridView绑定到DataTable。后来... 查看详情

将 DataTable 绑定到已定义列的 Datagridview

】将DataTable绑定到已定义列的Datagridview【英文标题】:BindDataTabletoDatagridviewthatalreadyhavecolumndefined【发布时间】:2013-06-2014:53:25【问题描述】:我正在尝试将DateTable绑定到Datagridview已经有在VS中使用Designer设计的列。DataTable的来源... 查看详情

如何将 DataTable.Select() 的结果绑定到 ListBox 控件?

】如何将DataTable.Select()的结果绑定到ListBox控件?【英文标题】:HowdoIbindtheresultofDataTable.Select()toaListBoxcontrol?【发布时间】:2010-09-1200:09:08【问题描述】:我有以下代码:ListBox.DataSource=DataSet.Tables("table_name").Select("some_criteria=match")... 查看详情

如何将 DataTable 绑定到由 DatagridViewTextBoxColumn 和 DataGridViewComboBoxColumn 组成的 DataGridView?

】如何将DataTable绑定到由DatagridViewTextBoxColumn和DataGridViewComboBoxColumn组成的DataGridView?【英文标题】:HowtobindDataTabletoDataGridViewthatconsistsofaDatagridViewTextBoxColumnandaDataGridViewComboBoxColumn?【发布时间】:2014-01-2807:48:01 查看详情

将 DataTable 绑定到 RDLC 和 ReportViewer

】将DataTable绑定到RDLC和ReportViewer【英文标题】:BindDataTabletoRDLCandReportViewer【发布时间】:2014-03-0723:49:06【问题描述】:我已经阅读了所有关于此的SO问题和在线文章,但我在几个不同的情况下感到困惑。在我的项目中,我尝试... 查看详情

DataGridView 绑定到已键入的 DataTable 时显示空单元格

】DataGridView绑定到已键入的DataTable时显示空单元格【英文标题】:DataGridViewdisplaysemptycellswhenboundtotypedDataTable【发布时间】:2009-09-2915:06:17【问题描述】:我正在创建一个简单的应用程序,它简单地使用强类型DataTable填充DataGridVie... 查看详情

由于使用自动增量添加到表中,如何使用更新的数据刷新 C# DataTable?

...于使用自动增量添加到表中,如何使用更新的数据刷新C#DataTable?【英文标题】:HowtorefreshaC#DataTablewithupdateddataduetoaddingtotablewithautoincrement?【发布时间】:2015-03-2321:40:52【问题描述】:将数据推送到数据库后,如何用新更新的数... 查看详情

C# 将 DataTable 绑定到现有 DataGridView 列定义

】C#将DataTable绑定到现有DataGridView列定义【英文标题】:C#BindDataTabletoExistingDataGridViewColumnDefinitions【发布时间】:2011-02-1304:09:09【问题描述】:我一直在与NullReferenceException作斗争,希望这里有人能够为我指明正确的方向。我正在... 查看详情

如何在已经绑定数据源的datagridview添加一行数据并保存到数据库??

网上有人说可以加一行这个就好((DataTable)dataGridView1.DataSource).Rows.Add("row");可是调试结果显示无法将类型为“System.Windows.Forms.BindingSource”的对象强制转换为类型“System.Data.DataTable”。求大神帮忙给出详细解答(本人完全菜... 查看详情