C# 使DataGridView直观地反映其数据源中的更改的正确方法

C# 使DataGridView直观地反映其数据源中的更改的正确方法,c#,winforms,ado.net,datagridview,C#,Winforms,Ado.net,Datagridview,假设DataGridView将其DataSource属性设置为DataView实例 DataGridView dgv; DataTable dt; // ... dt gets populated. DataView dv = dt.DefaultView; dgv.DataSource = dv; // ... dt gets modified // The DataGridView needs to update to show these changes visually //

假设
DataGridView
将其
DataSource
属性设置为
DataView
实例

DataGridView dgv;
DataTable dt;

// ... dt gets populated.

DataView dv = dt.DefaultView;

dgv.DataSource = dv;

// ... dt gets modified

// The DataGridView needs to update to show these changes visually
// What goes here?

我知道您可以将
dgv.DataSource
设置为
null
,然后返回到
dv
。但这似乎很奇怪。我相信还有其他几种方法。但是正确的官方方法是什么呢?

正确的方法是数据源实现
IBindingList
,为
SupportsAngeNotification
返回
true
,并发出
ListChanged
事件。但是,一个
DataView
可以做到这一点…

正确的方法是数据源实现
IBindingList
,为
SupportsChangeNotification
返回
true
,并发出
ListChanged
事件。但是,好吧,
DataView
会这样做……

我很确定,如果您的DataGridView绑定到DataTable的DefaultView,并且表发生了更改,那么更改会自动反映在DataGridView中。您是否尝试过此方法,但遇到了问题?发布更新数据表的代码,可能是其他错误。事实上,我刚刚编写了一个小样本应用程序:

public partial class Form1 : Form
    {
        private DataTable table;

        public Form1()
        {
            InitializeComponent();
            table = new DataTable();
            this.LoadUpDGV();
        }

        private void LoadUpDGV()
        {
            table.Columns.Add("Name");
            table.Columns.Add("Age", typeof(int));

            table.Rows.Add("Alex", 27);
            table.Rows.Add("Jack", 65);
            table.Rows.Add("Bill", 22);
            table.Rows.Add("Mike", 36);
            table.Rows.Add("Joe", 12);
            table.Rows.Add("Michelle", 43);
            table.Rows.Add("Dianne", 67);

            this.dataGridView1.DataSource = table.DefaultView;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            table.Rows.Add("Jake", 95);
        }
    }

基本上,当表单加载时,它只是用名称和年龄填充表。然后将其绑定到DGV。单击按钮时,它会将另一行添加到数据表本身。我对它进行了测试,果然它在网格中出现了,没有任何问题

我非常确定,如果您的DataGridView绑定到DataTable的DefaultView,并且表发生了更改,那么更改会自动反映在DataGridView中。您是否尝试过此方法,但遇到了问题?发布更新数据表的代码,可能是其他错误。事实上,我刚刚编写了一个小样本应用程序:

public partial class Form1 : Form
    {
        private DataTable table;

        public Form1()
        {
            InitializeComponent();
            table = new DataTable();
            this.LoadUpDGV();
        }

        private void LoadUpDGV()
        {
            table.Columns.Add("Name");
            table.Columns.Add("Age", typeof(int));

            table.Rows.Add("Alex", 27);
            table.Rows.Add("Jack", 65);
            table.Rows.Add("Bill", 22);
            table.Rows.Add("Mike", 36);
            table.Rows.Add("Joe", 12);
            table.Rows.Add("Michelle", 43);
            table.Rows.Add("Dianne", 67);

            this.dataGridView1.DataSource = table.DefaultView;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            table.Rows.Add("Jake", 95);
        }
    }
基本上,当表单加载时,它只是用名称和年龄填充表。然后将其绑定到DGV。单击按钮时,它会将另一行添加到数据表本身。我对它进行了测试,果然它在网格中出现了,没有任何问题