zoukankan      html  css  js  c++  java
  • DataGridView 实时更新表格数据

    悲伤 不想说话

    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

        public partial class FDataGridView : Form  // 在窗体上拖一个DataGridView上去
        {
            // 1) 实现System.ComponentModel.INotifyPropertyChanged接口
            class Character : INotifyPropertyChanged
            {
                string _name;
                [DisplayName("你的名字")]  // 这是显示的列名称哦
                public string Name
                {
                    get { return _name; }
                    set
                    {
                        if (_name != value)
                        {
                            _name = value;
                            NotifyPropertyChanged("Name");
                        }
                    }
                }
    
                // 2) 在属性改变时引发事件
                int _age;
                [DisplayName("他的年纪")]
                public int Age
                {
                    get { return _age; }
                    set
                    {
                        if (_age != value)
                        {
                            _age = value;
                            NotifyPropertyChanged("Age");
                        }
                    }
                }
    
                PropertyChangedEventHandler _propertyChanged;
                public event PropertyChangedEventHandler PropertyChanged
                {
                    add { _propertyChanged += value; }
                    remove { _propertyChanged -= value; }
                }
    
                private void NotifyPropertyChanged(string property_name)
                {
                    _propertyChanged?.Invoke(this, new PropertyChangedEventArgs(property_name));
                }
            }  /* end of class Character */
    
            DataTable _dtSource;   // 方式一
    
            BindingList<Character> _bdList;  // 方式二
            BindingSource _bdSource;
    
            public FDataGridView()
            {
                InitializeComponent();
    
                FormClosing += FDataGridView_FormClosing;
    
                Menu = new MainMenu();
                MenuItem operation = new MenuItem("Operation");
    
                Menu.MenuItems.Add(operation);
    
                MenuItem menu1 = new MenuItem("DataTable");
                MenuItem menu11 = new MenuItem("Bind DataSource", menu11_Click);
                MenuItem menu12 = new MenuItem("Change Data", menu12_Click);
                MenuItem menu13 = new MenuItem("Create Data", menu13_Click);
                menu1.MenuItems.Add(menu11);
                menu1.MenuItems.Add(menu12);
                menu1.MenuItems.Add(menu13);
    
                MenuItem menu2 = new MenuItem("BindingSource");
                MenuItem menu21 = new MenuItem("Bind DataSource", menu21_Click);
                MenuItem menu22 = new MenuItem("Change Data", menu22_Click);
                MenuItem menu23 = new MenuItem("Create Data", menu23_Click);
                menu2.MenuItems.Add(menu21);
                menu2.MenuItems.Add(menu22);
                menu2.MenuItems.Add(menu23);
    
                operation.MenuItems.Add(menu1);
                operation.MenuItems.Add(menu2);
    
                Init();
            }
    
            private void FDataGridView_FormClosing(object sender, FormClosingEventArgs e)
            {
                _dtSource.Dispose();
                _bdList.Clear();
                _bdSource.Dispose();
    
                _dtSource = null;
                _bdList = null;
                _bdSource = null;
            }
    
            void Init()
            {
                _dtSource = new DataTable();
                _bdList = new BindingList<Character>();
                _bdSource = new BindingSource();
                _bdSource.DataSource = _bdList;  // 3) 绑定数据源,由BindingSoource组件处理细节
    
                _dtSource.Columns.Add(new DataColumn("Name"));
                _dtSource.Columns.Add(new DataColumn("Age"));
    
                DataRow dr;
    
                dr = _dtSource.NewRow();
                dr["Name"] = "1";
                dr["Age"] = "21";
                _dtSource.Rows.Add(dr);
    
                dr = _dtSource.NewRow();
                dr[0] = "2";
                dr[1] = "23";
                _dtSource.Rows.Add(dr);
    
                _dtSource.Rows.Add(3, 27);
    
                _bdList.Add(new Character() { Name = "1", Age = 21 });
                _bdList.Add(new Character() { Name = "2", Age = 23 });
                _bdList.Add(new Character() { Name = "3", Age = 27 });
            }
    
            #region menu click
            private void menu11_Click(object sender, EventArgs e)
            {
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = _dtSource;
            }
    
            private void menu12_Click(object sender, EventArgs e)
            {
                _dtSource.Rows[0][1] = new Random().Next(0, 255);
            }
    
            private void menu13_Click(object sender, EventArgs e)
            {
                _dtSource.Rows.Add(Guid.NewGuid().ToString("N"), "en");
            }
    
            private void menu21_Click(object sendr, EventArgs e)
            {
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = _bdSource;
            }
    
            private void menu22_Click(object sender, EventArgs e)
            {
                _bdList[0].Age = new Random().Next(0, 255);
            }
    
            private void menu23_Click(object sender, EventArgs e)
            {
                _bdList.Add(new Character() { Name = Guid.NewGuid().ToString("N") });
            }
            #endregion
    
            private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
            {
                ;// dataGridView1.DataError += dataGridView1_DataError;  处理表格异常
            }
    
            private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                if (e.RowIndex >= 0)
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, Brushes.Red, e.RowBounds.X + 15, e.RowBounds.Y + 10);
            }
    
    
        }  /* end of class */
    View Code
  • 相关阅读:
    [Kotlin] Open Classes and Inheritance
    [Kotlin] Class
    [Kotlin] Reverse a List with downTo
    [Kotlin] Named loop
    [Kotlin] for loop
    [Kotlin] Array List ArrayList
    深度解读 java 线程池设计思想及源码实现
    源码实战 | 从线程池理论聊聊为什么要看源码
    自己实现一个简单的线程池
    死磕 java线程系列之自己动手写一个线程池(续)
  • 原文地址:https://www.cnblogs.com/xuanhu/p/9795547.html
Copyright © 2011-2022 走看看