Checking the value of a cell in a dataGridView

Hello experts,

I am trying to find a way to check if a cell inside a dataGridvue has a value (not blank).

I tried the following line of code without success:

if (this.dataGridView1.Rows[selectedRow].Cells[3].Value != null)

The real problem is an error that comes about when the user tries to delete a row that's has no data.
 I thought the best way to go about this is to find if one of the cells in the row (which must contain data) is in fact empty. So I tried the conditional code below, which, obviously, doesn't work.

Help...!
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 toDeleteRows = e.RowIndex;
            Int32 toUpdateColumns = e.ColumnIndex;
            if (toDeleteRows > -1 && toUpdateColumns == 0)
            {
                DialogResult result = MessageBox.Show("You are about to delete the record", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                   
                   if (this.dataGridView1.Rows[toDeleteRows].Cells[3].Value != null)
                    {
                        this.dataGridView1.Rows.RemoveAt(toDeleteRows);
                        deleteRow();
                   }
                       else
                    {
                        MessageBox.Show("Nothing to delete", "Information");
                    }

Open in new window

adamtraskAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nepaluzCommented:
remove line 14, i.e
deleteRow();

Open in new window

then encapsulate the routine in a try ... catch to see what error you get (you may need to handle the DataError event of the datagridview)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
Try changing

if (this.dataGridView1.Rows[toDeleteRows].Cells[3].Value != null)

to

if (IsDBNull(this.dataGridView1.Rows[toDeleteRows].Cells[3].Value))
0
adamtraskAuthor Commented:
nepaluz: deleteRow(); Calls the method that deletes the relevant record from the databse table after it's deleted in the grid.


CodeCruiser: I tried the code you suggested but I got a red line under "isDBNul" that says:

 the name IsDBNull doesn't exist in the current context.

0
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

CodeCruiserCommented:
0
adamtraskAuthor Commented:
Well.... I came up with the following solution and it seems to work. Please advise if there is a better one:


private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 toDeleteRows = e.RowIndex;
            Int32 toUpdateColumns = e.ColumnIndex;
            if (toDeleteRows > -1 && toUpdateColumns == 0)
            {
                DialogResult result = MessageBox.Show("You are about to delete the record", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                   // this.txtID.Text = this.dataGridView1.Rows[toDeleteRows].Cells[2].Value.ToString();
                    string stringCheck = this.dataGridView1.Rows[toDeleteRows].Cells[3].Value.ToString();
                   if (stringCheck != string.Empty)
                   {
                        this.dataGridView1.Rows.RemoveAt(toDeleteRows);
                      deleteRow();
                   }
                       else
                    {
                        MessageBox.Show("Nothing to delete", "Information");
                    }

Open in new window

0
Spider-MonkeyCommented:
Can you post the result of the try catch block exception. I also noticed that you haven't defined what event your method handles. Try defining what the event handles using the Handles keyword. Not to certain on the C# syntax so correct me if I am wrong.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) Handles Datagridview1.CellContentClick

Open in new window

0
adamtraskAuthor Commented:
I tried covert.IsDbNull as shown below but I get the following error:
InvalidOpertaionException was unhandled
Uncommitted new row cannot be deleted

As far as I can tell, the application is still trying to delete the blank row!

if (Convert.IsDBNull(this.dataGridView1.Rows[toDeleteRows].Cells[3].Value))
                   {
                        this.dataGridView1.Rows.RemoveAt(toDeleteRows);
                      deleteRow();
                   }
                       else
                    {
                        MessageBox.Show("Nothing to delete", "Information");
                    }

Open in new window

0
nepaluzCommented:
is the datagridview databound?
0
adamtraskAuthor Commented:
No, the dataGridView is not databound. I use code to get data from an Sql database table into the dataGridview.....

The code which I posted above works fine.... I just want to make sure there isn't a better solution. Here it is again below:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 toDeleteRows = e.RowIndex;
            Int32 toUpdateColumns = e.ColumnIndex;
            if (toDeleteRows > -1 && toUpdateColumns == 0)
            {
                DialogResult result = MessageBox.Show("You are about to delete the record", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    this.txtID.Text = this.dataGridView1.Rows[toDeleteRows].Cells[2].Value.ToString();
                  string stringCheck = this.dataGridView1.Rows[toDeleteRows].Cells[3].Value.ToString();
                   if (stringCheck != string.Empty)
                   // if (Convert.IsDBNull(this.dataGridView1.Rows[toDeleteRows].Cells[3].Value))
                   {
                        this.dataGridView1.Rows.RemoveAt(toDeleteRows);
                      deleteRow();
                   }
                       else
                    {
                        MessageBox.Show("Nothing to delete", "Information");
                    }

                }
                if (toDeleteRows > -1 && toUpdateColumns == 1)
                {
                    DialogResult outCome = MessageBox.Show("You are about to update record", "Confirmation", MessageBoxButtons.OKCancel);
                    if (outCome == System.Windows.Forms.DialogResult.OK)
                    {
                        MessageBox.Show("Got you");
                    }

Open in new window

0
adamtraskAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.