none
current cell is always null no matter what have been typed in the cell. RRS feed

  • Question

  • Dear all,

            I have the following code where the value of current cell is always null no matter what have been typed in the cell. I have checked the rowindex and

    columnindex , both are 0. And that is correct for the first cell in first row. It seems the Leave event cannot get the value that I have typed. How to solve it? Thanks.

    private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
            {
                DataSet ds1 = new DataSet();
                DataSet ds2 = new DataSet();

                if ((dataGridView1.CurrentCell.Value==null ) )
                {
                    MessageBox.Show("Please input something");
                    return;
                }

                }


    hon123456
    Saturday, January 22, 2011 6:53 AM

Answers

  • OK, no worry, maybe the cell's value still is not valid, please try CellValidated event instead i.e.
    private void DataGridView1_CellValidated(object sender,
      DataGridViewCellEventArgs e)
    {
          if ((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 7:43 AM
    Saturday, January 22, 2011 7:38 AM

All replies

  • Hi,

    According to it's documentation:

    "DataGridView.CellLeave Event

    Occurs when a cell loses input focus and is no longer the current cell."
    So, please try this instead:
    private void dataGridView1_CellLeave(object sender, 
     DataGridViewCellEventArgs e)
    {
     if(null==dataGridView1[e.ColumnIndex, e.RowIndex].Value || DBNull.Value.Equals(dataGridView1[e.ColumnIndex, e.RowIndex].Value)){/*stuff here*/}
    }
    
    
    Your Sincerely,
    Yasser.

    LEARN HOW TO USE WINDOWS API DURING A QUICK, SIMPLE AND PRACTICAL HOW TO:
    How To: Changing TextBox blinking caret using Windows API
    Saturday, January 22, 2011 7:17 AM
  • Dear all,

          I have tried the following code

              private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
            {

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

                else
               {    CheckStockcode()
               }

    I have input  "123" into the cell.  But now it is still return null for  dataGridView1[e.ColumnIndex,e.RowIndex].Value. Please help. Thanks.

     


    hon123456
    Saturday, January 22, 2011 7:29 AM
  • OK, no worry, maybe the cell's value still is not valid, please try CellValidated event instead i.e.
    private void DataGridView1_CellValidated(object sender,
      DataGridViewCellEventArgs e)
    {
          if ((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 7:43 AM
    Saturday, January 22, 2011 7:38 AM
  • Thanks very much Yasser. Solved as your suggestion.
    hon123456
    Saturday, January 22, 2011 7:43 AM
  • Thanks very much Yasser. Solved as your suggestion.
    hon123456

    You're welcome, have a nice time :)
    LEARN HOW TO USE WINDOWS API DURING A QUICK, SIMPLE AND PRACTICAL HOW TO:
    How To: Changing TextBox blinking caret using Windows API
    Saturday, January 22, 2011 7:45 AM