DataGridView binding correct posture of the DataTable

1. DataTable bound to the BindingSource

2. BindingSource bound to DataGridView

3. DataGridView when finished modifying the value from Datatable, synchronous past, BindingSource and DataGridView two have to perform EndEdit ()

Routine:

 public partial class Form1 : Form
    {
        DataTable mTable = new DataTable();
        BindingSource mbs = new BindingSource();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            mTable.Columns.Add("Name",typeof(string));
            mTable.Columns.Add("Age", typeof(float));
            mTable.Columns.Add("Dept", typeof(string));
            mTable.Columns.Add("IsDeleted", typeof(bool));
            mTable.Rows.Add("Jack", 21, "C1", false);
            mTable.Rows.Add("Rose", 21, "C2", false);
            mTable.Rows.Add("Tom", 21, "C1", false);
            mTable.Rows.Add("Micky", 21, "C1", false);
            mTable.Rows.Add("Steven Chou", 21, "C1", false);
            mbs.DataSource = mTable;
            grd.DataSource = mbs;
        }

        private void BtnDel_Click(object sender, EventArgs e)
        {
            mTable.Rows.Remove(mTable.Rows[grd.CurrentRow.Index]);
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            grd.EndEdit();
            mbs.EndEdit();

            int N = mTable.Rows.Count;
            
        }
    }

 

Guess you like

Origin www.cnblogs.com/ccjungle/p/11308195.html