C# Datagridview 不更新/刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13231149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:50:32  来源:igfitidea点击:

Datagridview not updating/refreshing

c#winformsgridview

提问by user966614

I have a DataGridView made of a DataSet of a table from the DB. When I delete a row, it is updated in the database but it is not removed from the GridView. Only when I restart the application does it get removed from the GridView.

我有一个由数据库中的表的 DataSet 组成的 DataGridView。当我删除一行时,它会在数据库中更新,但不会从 GridView 中删除。只有当我重新启动应用程序时,它才会从 GridView 中删除。

Please help

请帮忙

采纳答案by Derek

This is a very simple process.

这是一个非常简单的过程。

1.) Create a Binding Source

2.) Set the Datasource for this object to your Dataset Table.

3.) Set The datasource for your DatagridView as the Binding source Object.

1.) 创建绑定源

2.) 将此对象的数据源设置为您的数据集表。

3.) 将 DatagridView 的数据源设置为绑定源对象。

Code Example:

代码示例:

Dataset ds = new Dataset();
BindingSource bs = new BindingSource()
bs.Datasource = ds.Table[0];
DatagridView.Datasource = bs;

Now, Any changes you make in the DataTable will ripple through to your GridView automatically.

现在,您在 DataTable 中所做的任何更改都会自动影响到您的 GridView。

回答by Sylca

Hope this helps you?

希望这对你有帮助?

if you are showing your table in dgv and for deleting something from that table you have a button on your win form. you chose let's say ID and click "delete" button for delting an item from db table. Here is that code:

如果您在 dgv 中显示您的表格并从该表格中删除某些内容,则您的 win 表单上有一个按钮。您选择了 ID,然后单击“删除”按钮从 db 表中删除一个项目。这是代码:

private void btn_Delete_Click(object sender, EventArgs e)
        {
            int selectedCellCount = dgv.GetCellCount(DataGridViewElementStates.Selected);

            if (selectedCellCount > 0)
            {
                string selection;

                for (int i = 0; i < selectedCellCount; i++)
                {
                    selection = dgv.SelectedCells[i].Value.ToString();
                    string qs_delete = "DELETE FROM yor_table WHERE id = '" + selection + "';";
                    try
                    {
                        conn = new MySqlConnection(cs);
                        conn.Open();

                        cmd = new MySqlCommand();
                        cmd.Connection = conn;
                        cmd.CommandText = qs_delete;
                        cmd.ExecuteNonQuery();

                        conn.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                           if (conn != null) conn.Close();
                         }
                     }
                }    

//don't forget to load your table again in dgv
            string qs_select = "SELECT * FROM your_table";

            System.Data.DataTable dataTable = new System.Data.DataTable();
            dataTable.Clear();
            dgv.DataSource = dataTable;

            try
            {
                conn = new MySqlConnection(cs);
                cmd = new MySqlCommand(qs_select, conn);
                conn.Open();

                da = new MySqlDataAdapter(cmd);
                da.Fill(dataTable);

                cb = new MySqlCommandBuilder(da);

                dgv.DataSource = dataTable;
                dgv.DataMember = dataTable.TableName;
                dgv.AutoResizeColumns();

                conn.Close();
            }
            catch (Exception ex)
            {
                 MessageBox.Show(ex.Message);
            }
            finally
            {
                if (conn != null) conn.Close();
            }
         }

You'll notice here that i have MySQL db but don't bother yourself with that.

您会在这里注意到我有 MySQL 数据库,但不要为此烦恼。

回答by Sylca

You have to call this function after every deletion(Re-bind Grid).

您必须在每次删除后调用此函数(重新绑定网格)。

void BindGrid()
{
YourDataGridView.DataSource = dataset;
YourDataGridView.DataBind();
}

回答by helgeheldre

You need to reset the binding on your bindingsource.

您需要重置绑定源上的绑定。

bindingSource.ResetBindings(false);

回答by Derek

If database is updated and you want to refresh DataGridView, call this:

如果数据库已更新并且您想刷新 DataGridView,请调用:

this.<table name>TableAdapter.Fill(this.<DB name>DataSet.<table name>);

For example: Where is name of your table (for example Customers) and is name of your database (for example MyDB).

例如:其中是您的表名(例如客户)和您的数据库名称(例如 MyDB)。

this.CustomersTableAdapter.Fill(this.MyDBDataSet.Customers);