none
How to stop validation in the last new row of datagridview RRS feed

  • Question

  • Dear all,

              When I type in some text in a cell and press Enter,then a new blank row will be shown in the datagridview. However , I have some validating code in cell_validated event. I want the code not to be run when there is the last row( will not type anything in last row). How can I do it ? Thanks.
    The following code is run even when I leave the last blank row created by grid.

              private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
            {

                if ((dataGridView1[e.ColumnIndex,e.RowIndex].Value==null ) )
                {
                    MessageBox.Show("請輸入貨品編號");
                    return;
                }

                else
               {    CheckStockcode()
               }


    hon123456
    Saturday, January 22, 2011 8:26 AM

Answers

  • Hi again,

    Try

         private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
    
          if (!dataDridView1.Rows[e.RowIndex].IsNewRow &&(dataGridView1[e.ColumnIndex,e.RowIndex].Value==null ) )
          {
            MessageBox.Show("請輸入貨品編號");
            return;
          }
    
          else 
          {  CheckStockcode()
          }
    
    

    LEARN HOW TO USE WINDOWS API DURING A QUICK, SIMPLE AND PRACTICAL HOW TO:
    How To: Changing TextBox blinking caret using Windows API
    • Marked as answer by hi123456 Saturday, January 22, 2011 9:17 AM
    Saturday, January 22, 2011 8:36 AM

All replies

  • Hi again,

    Try

         private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
    
          if (!dataDridView1.Rows[e.RowIndex].IsNewRow &&(dataGridView1[e.ColumnIndex,e.RowIndex].Value==null ) )
          {
            MessageBox.Show("請輸入貨品編號");
            return;
          }
    
          else 
          {  CheckStockcode()
          }
    
    

    LEARN HOW TO USE WINDOWS API DURING A QUICK, SIMPLE AND PRACTICAL HOW TO:
    How To: Changing TextBox blinking caret using Windows API
    • Marked as answer by hi123456 Saturday, January 22, 2011 9:17 AM
    Saturday, January 22, 2011 8:36 AM
  • Thanks very much,Yasser. It solved again. Thanks.
    hon123456
    Saturday, January 22, 2011 9:17 AM