wpf的datagrid和winform的datagridview刷新

DataGrid的数据源的加载需要大量IO操作,不可能等数据全部读取之后才显示到UI上。由于对WPF数据绑定不很熟悉,对ObserveCollection等内容没有太多时间去研究,只能用一些取巧的办法了。

设置DataGrid的数据源,只要修改ItemsSource属性就可以了,如下:

List dataList = new List()

datagrid1.ItemsSource = dataList;

但是如果没有进行数据绑定的话,对dataList进行Add,是不会更新的UI上的,除非点击列标题,对列进行排序,UI线程对数据显示的更新。主动的做法是使用Refresh方法,如下:

dataGrid1.Items.Refresh();

 

winform的datagridview要使用这个绑定

        List lstNode = new List();

        private BindingSource customersBindingSource = new BindingSource();

        private void Form1_Load(object sender, EventArgs e)

        {

            Node n1 = new Node { UserName = "linux", InstrumentID = "if", Direction = "买" };

            lstNode.Add(n1);

            this.dataGridView1.DataSource = lstNode;



            //var blist = new BindingList(lstNode);

            //d/ataGridView1.DataSource = blist;

            this.customersBindingSource.DataSource = lstNode;

            this.dataGridView1.DataSource = this.customersBindingSource;

        }



        private void button1_Click(object sender, EventArgs e)

        {

            Node n1 = lstNode[0];

            n1.UserName = "dd";

            Node n11 = new Node { UserName = "linux111", InstrumentID = "if11", Direction = "买22" };

            lstNode.Add(n11);

            customersBindingSource.ResetBindings(true);

           

        }

如果使用datatable则可以直接更新

你可能感兴趣的:(datagridview)