• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 765
  • Last Modified:

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

0
adamtrask
Asked:
adamtrask
  • 5
  • 2
  • 2
  • +1
3 Solutions
 
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
 
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now