Question : datagridview cell click of null value

Experts
What I have to write here to avoid the errors inside the if loop

public void dataGridView5_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((dataGridView5.CurrentRow.Cells[2].Value != null)||(dataGridView5.CurrentRow.Cells[2].Value.ToString() != "0"))
            {
                filmid = Convert.ToInt32(dataGridView5.CurrentRow.Cells[2].Value);
                dt = Convert.ToDateTime(dataGridView5.CurrentRow.Cells[5].Value);
                InsertIntoList(filmid, dt);
            }
            else
            {
                MessageBox.Show("There is no Item Borrowed.","No Item Borrowed",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            }
                       
        }


For further clarification of my question please refer the images attached
Thanking you
Attachments:
 
 
 
2
2
 

Answer : datagridview cell click of null value

the best way to validate users' input data is to use the validation of Validation events of datagrid as
attached in code hopes to be clear
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
        private void dataGridView5_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            switch(e.ColumnIndex)
            {
                case 2:
                    DataGridViewCell currentCell = dataGridView5[e.ColumnIndex, e.RowIndex];
                    currentCell.ErrorText = "";
                    int x;
                    if ( int.TryParse(currentCell.Value.ToString(),out x) )
                    {
                        if (x == 0)
                        {
                            currentCell.ErrorText = "Value should not be zero";
                        }
                    }
                    else
                    {
                        currentCell.ErrorText = "Please enter a valid integer value";
                    }
                break;
            }
        }
Random Solutions  
 
programming4us programming4us