Link to home
Start Free TrialLog in
Avatar of ANINDYA
ANINDYAFlag for India

asked on

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
error.JPG
1.JPG
Avatar of Ayman Aboualnour
Ayman Aboualnour
Flag of Egypt image

First of all use CellEndEdit Event
then put:
try and catch
try
{
//your if condition and code
}
catch(Exception exp)
{

}

//you can put a break point to get the value of Cell[x] to handle it's exception
Avatar of ANINDYA

ASKER

Expert aymoon80

I have done as per your suggestion and it is working perfectly .
Thanks for your suggestion  and help.
Sir I have a small question
Why you asked me to use that CellEndEdit Event?
Thanking you
Anindya
Avatar of ANINDYA

ASKER

Sir
The cell click is necessary as otherwise I would not get the values of the
filmid and dt as you can see in the code.
Please help
Thanking you
Anindya
Thanks, I ask you to use CellEndEdit event to handle the data after changes not before changing it.
I'll give you the code to get the cell value later to check this by myself.

Regards,
Ayman
but what is the datasource of your datagrid is that a datatable or other thing?
Avatar of ANINDYA

ASKER

Expert  aymoon80
It is a data table.
As you can see the code here I have shown in the attached image.
Please see.


 private void Form_Search_Items_Borrowed_Load(object sender, EventArgs e)
        {
            //string query = "select * from Table_Borrow where MemberID=@parameter1";
            string b = "Borrowed";
            string query = "select * from Table_Borrow where MemberID=@parameter1 and Status LIKE '" + b.ToString() + "' ";
            using (SqlConnection con = new SqlConnection("Data Source=INVENTOR-6FBADA\\SQLEXPRESS;Initial Catalog=AnindyaCD;Integrated Security=SSPI"))
            {
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.AddWithValue("@parameter1", MemberID);

                    using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                    {
                        ad.SelectCommand = cmd;
                        DataSet ds = new DataSet();
                        ad.Fill(ds, "Table_Borrow");
                        dataGridView5.DataSource = ds.Tables[0];
                    }
                }
            }
           

        }
        public Int32 filmid { get; set; }
        public DateTime dt { get; set; }
        //public static Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
        //public static Dictionary<int, DateTime> FilmSelectDictionary   {get;set;}
        Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
        public Dictionary<int, DateTime> FilmSELECTDictionary
        {
            get { return FilmSelectDictionary; }
        }
        public void dataGridView5_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                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);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
           
                       
        }
 private void Form_Search_Items_Borrowed_Load(object sender, EventArgs e)
        {
            //string query = "select * from Table_Borrow where MemberID=@parameter1";
            string b = "Borrowed";
            string query = "select * from Table_Borrow where MemberID=@parameter1 and Status LIKE '" + b.ToString() + "' ";
            using (SqlConnection con = new SqlConnection("Data Source=INVENTOR-6FBADA\\SQLEXPRESS;Initial Catalog=AnindyaCD;Integrated Security=SSPI"))
            {
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.AddWithValue("@parameter1", MemberID);

                    using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                    {
                        ad.SelectCommand = cmd;
                        DataSet ds = new DataSet();
                        ad.Fill(ds, "Table_Borrow");
                        dataGridView5.DataSource = ds.Tables[0];
                    }
                }
            }
            

        }
        public Int32 filmid { get; set; }
        public DateTime dt { get; set; }
        //public static Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
        //public static Dictionary<int, DateTime> FilmSelectDictionary   {get;set;}
        Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
        public Dictionary<int, DateTime> FilmSELECTDictionary
        {
            get { return FilmSelectDictionary; }
        }
        public void dataGridView5_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                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);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
                       
        }

Open in new window

error.JPG
ASKER CERTIFIED SOLUTION
Avatar of Ayman Aboualnour
Ayman Aboualnour
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the event name CellValidating event of your datagrid also you can switch on Column Index to make many validations.
also you can use Column name instead of Column Index to avoid problems when changing the columns order in your query.
you can use the same technique in dataGridView5_CellClick
Avatar of ANINDYA

ASKER

Expert aymoon80

Thanking you very much for the total code and also the help which you have rendered towards me .
i am delighted.
Thanks for your patience and valuable time.
Thanking you,
Anindya Chatterjee
Bangalore
India
Avatar of ANINDYA

ASKER

Expert   aymoon80
I am thankful to you .
I have a request that is if in future I have questions can you help me.
If yes then please  let me know how to provide you the URL of my question .
I want to provide you the urls and if you find a trifle time from your busy schedule then you may answer them.
Thanking you
Anindya Chatterjee
Bangalore
India
Of course, I'm following your activities on experts-exchange so I'll follow your questions here.
Best Regards,
Ayman