急救        如何在DataGridView控件中验证数据输入  数据输入错误时,弹出错误提示!~~~~~~~~~~~~~~~~~在线等待!!!
 源码

解决方案 »

  1.   

    可以处理DataGridView的DataError事件,例如 :private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
            {
                if (this.dataGridView1.Rows[0].Cells[0].Value.ToString() == "")
                {
                    MessageBox.Show("第一行第一列的值不能为空!");
                }
            }
    不知这样能不能满足你的需求!
      

  2.   

    处理DataGridView的DataError事件楼上正解
      

  3.   

    dataerror事件是在验证失败后出现的,应该处理RowValidating事件
      

  4.   

    验证多列是一样的啊,RowValidating是验证一行数据的
      

  5.   

    RowValidating和CellValidating联合验证
    比较简单
    lz自己试一下就知道了
      

  6.   

    //DataGridView中的数据类型验证,Msdn中的例子
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form
    {
        private DataGridView dataGridView1 = new DataGridView();
        private BindingSource bindingSource1 = new BindingSource();    public Form1()
        {
            // Initialize the form.
            this.dataGridView1.Dock = DockStyle.Fill;
            this.Controls.Add(dataGridView1);
            this.Load += new EventHandler(Form1_Load);
            this.Text = "DataGridView validation demo (disallows empty CompanyName)";
        }    private void Form1_Load(System.Object sender, System.EventArgs e)
        {
            // Attach DataGridView events to the corresponding event handlers.
            this.dataGridView1.CellValidating += new
                DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
            this.dataGridView1.CellEndEdit += new
                DataGridViewCellEventHandler(dataGridView1_CellEndEdit);        // Initialize the BindingSource and bind the DataGridView to it.
            bindingSource1.DataSource = GetData("select * from Customers");
            this.dataGridView1.DataSource = bindingSource1;
            this.dataGridView1.AutoResizeColumns(
                DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
        }    private void dataGridView1_CellValidating(object sender,
            DataGridViewCellValidatingEventArgs e)
        {
            // Validate the CompanyName entry by disallowing empty strings.
            if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName")
            {
                if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    dataGridView1.Rows[e.RowIndex].ErrorText =
                        "Company Name must not be empty";
                    e.Cancel = true;
                }
            }
        }    void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            // Clear the row error in case the user presses ESC.   
            dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
        }    private static DataTable GetData(string selectCommand)
        {
            string connectionString =
                "Integrated Security=SSPI;Persist Security Info=False;" +
                "Initial Catalog=Northwind;Data Source=localhost;Packet Size=4096";        // Connect to the database and fill a data table.
            SqlDataAdapter adapter =
                new SqlDataAdapter(selectCommand, connectionString);
            DataTable data = new DataTable();
            data.Locale = System.Globalization.CultureInfo.InvariantCulture;
            adapter.Fill(data);        return data;
        }    [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }}
      

  7.   

    //弹出错误提示
    private void dataGridView1_DataError(object sender,
        DataGridViewDataErrorEventArgs e)
    {
        // If the data source raises an exception when a cell value is 
        // commited, display an error message.
        if (e.Exception != null &&
            e.Context == DataGridViewDataErrorContexts.Commit)
        {
            MessageBox.Show("CustomerID value must be unique.");
        }
    }
      

  8.   

    你不会要别人都帮你写出来吧?既然能判断不能为空,就能判断是不是在这个范围内啊。
            private   void   dataGridView1_CellValidating(object   sender,
                    DataGridViewCellValidatingEventArgs   e)
            {
                    //   Validate   the   CompanyName   entry   by   disallowing   empty   strings.
                    if   (dataGridView1.Columns[e.ColumnIndex].Name   ==   "CompanyName")
                    {
                            try
    {
    int val = Int32.Parse(e.FormattedValue.ToString());
    if(val < 10 || val > 100)
    {
    dataGridView1.Rows[e.RowIndex].ErrorText   =
                                            "Company   Name   must   be   >10 and <100";
                                    e.Cancel   =   true;
    }
    }
                            catch(Exception ex){
                                    dataGridView1.Rows[e.RowIndex].ErrorText   =
                                            "Company   Name   must   be   number";
                                    e.Cancel   =   true; 
    }
                    }
            }