ullas K

ullas K

  • NA
  • 55
  • 0

Checking for datagridview cell entry and focussing control on the same cell if the entry is invalid

Aug 5 2012 6:59 AM
I am new to c#.net.I have a datagridview with columns ,issue date,bookno,book name.issue date and book name columns are locked.Now i enter a bookno in bookno column and here the following checking should be done.

1.Check whether the bookno column is empty
2.Check whether the bookno entered is valid and if valid the corresponding bookname is displayed in bookname column and if the value does not match it should show a error pop up message and the focus should be on the same cell . how to acheive this.Pls help me.In which event of datagridview should i write this This is my code in cell_validating event.but it is not working properly.It si retreiving the record if the entry is correct but if the entry does not match it is not working as desired

string bookno;
if ((dataGridView1.Rows[e.RowIndex].Cells[2].Value != null))
                        {
                            bookno = dataGridView1.CurrentRow.Cells[2].Value.ToString();


                            con = db.open();
                            getdata = new SqlCommand("select  book_no,E_Name from Books where Book_no = '" + bookno + "'", con);

                            readdata = getdata.ExecuteReader();
                            if (readdata.Read())
                           {
                                dataGridView1.CurrentRow.Cells[3].Value = readdata.GetValue(1).ToString();

                            }
                            else
                                if (!readdata.Read())
                                {

                                    MessageBox.Show("No Such Book Exists.Please check the Book Number", "easyLib", MessageBoxButtons.OK);
                                    e.Cancel = true;

                                }
                            db.close();

                        }

Answers (1)