获取编辑中的DataGridView.CurrentCell的值,并在CellValueChanged中做判断。

来源:互联网 发布:ipad登陆淘宝卖家在哪 编辑:程序博客网 时间:2024/06/05 09:57

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace experiment
{
    public partial class Form1 : Form
    {
        DataTable dt;
        public Form1()
        {
            InitializeComponent();
        }
       
        private void Form1_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            dt.Columns.Add("Column1");
            dt.Columns.Add("Column2");
            dt.Columns.Add("Column3");
            dt.Rows.Add("", "", "");
            dt.Rows.Add("", "", "");
            dt.Rows.Add("", "", "");
            dt.Rows.Add("", "", "");
            dataGridView1.DataSource = dt;
        }

 

        void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            dt.Rows[2][2] = dataGridView1.CurrentCell.EditedFormattedValue;
            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

 

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show("OK now");
        }

       
    }
}

 

 

 

参阅MSDN:ms-help://MS.MSDNQTR.v90.chs/fxref_system.windows.forms/html/bf0df27a-0b12-ea32-7341-8295a8abc102.htm

原创粉丝点击