locked
Comparing the cell value of a DataGridView to "", null or "No string"

    Question

  •         private void button1_Click(object sender, EventArgs e)
            {
                string wholeTimeValue;
                wholeTimeValue = dateTimePicker1.Value.ToString("HH:mm:tt");
    
                if (dataGridView1.Rows.Count == 1 && dataGridView1.Rows[0].Cells[0].Value.ToString() == "")
                {
                    dataGridView1.Rows[0].Cells[0].Value = wholeTimeValue;
                    dataGridView1.Rows[0].Cells[1].Value = 1;
                }
                else
                {
                    dataGridView1.Rows.Add(wholeTimeValue, "true");
                }
            }


    ??????

    How am I supposed to check for the unset cell?

    Alternatively I also tried:

    if (dataGridView1.Rows.Count == 1 && dataGridView1.Rows[0].Cells["Alarm"].Value.ToString() == "") and "Alarm" does not seem to be found.

    • Edited by S.e.p.y Sunday, July 29, 2012 10:46 PM
    Sunday, July 29, 2012 10:45 PM

Answers

  • I would use the static string.IsNullOrEmpty() method after checking to ensure that the cell has a value with something like this:

    private void button1_Click(object sender, EventArgs e)
    {
        string wholeTimeValue;
        wholeTimeValue = dateTimePicker1.Value.ToString("HH:mm:tt");
        if(dataGridView1.Rows.Count == 1)
        {
            var firstCell = dataGridView1.Rows[0].Cells[0];
            if(firstCell.Value != null)
            {
                if(string.IsNullOrEmpty(firstCell.Value.ToString()) == true)
                {
                    firstCell.Value = wholeTimeValue;
                    dataGridView1.Rows[0].Cells[1].Value = 1;
                }
            }
        }
    }

    • Marked as answer by S.e.p.y Tuesday, July 31, 2012 4:30 AM
    Monday, July 30, 2012 1:50 PM

All replies

  • Also I tried this:

    if (dataGridView1.Rows.Count == 1 && String.Equals(dataGridView1.Rows[0].Cells[0].Value.ToString(),"")) which leads to some error about how the new keyword must be used...

    Sunday, July 29, 2012 10:51 PM
  • I would use the static string.IsNullOrEmpty() method after checking to ensure that the cell has a value with something like this:

    private void button1_Click(object sender, EventArgs e)
    {
        string wholeTimeValue;
        wholeTimeValue = dateTimePicker1.Value.ToString("HH:mm:tt");
        if(dataGridView1.Rows.Count == 1)
        {
            var firstCell = dataGridView1.Rows[0].Cells[0];
            if(firstCell.Value != null)
            {
                if(string.IsNullOrEmpty(firstCell.Value.ToString()) == true)
                {
                    firstCell.Value = wholeTimeValue;
                    dataGridView1.Rows[0].Cells[1].Value = 1;
                }
            }
        }
    }

    • Marked as answer by S.e.p.y Tuesday, July 31, 2012 4:30 AM
    Monday, July 30, 2012 1:50 PM
  • In my code:

    if(firstCell.Value != null)

    and, yes so the Value of the first cell is null then. So it should be easy enough for me to modify the code to add or update the cell and remove any tailing empty rows which is really what I have been trying to do. Thank you for the code. I will post my code once I fugure it out. I believe you have the answer.

    Monday, July 30, 2012 10:33 PM
  • I'm having kind of an interesting time of it with this code:

           private void button1_Click(object sender, EventArgs e)
            {
                string wholeTimeValue;
                wholeTimeValue = dateTimePicker1.Value.ToString("HH:mm:tt");
    
                dataGridView1.Rows.Add(wholeTimeValue);
                dataGridView1.Rows[dataGridView1.Rows.Count - 2].Selected = true;
                dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count);
            }

    I'm finding that dataGridView1.Rows.Count - 2].Selected = true is actually selecting the first row which is highlighted blue and that dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count); actually results in an out of bounds error.

    I then tried dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1); and the environment is telling me that the specified row is uncommitted and that I can not remove it. So it has to be a committed row for it to be removed? I missing certain key points. Does anyone have the answers? The idea I have is to just remove the blank row which is always trailing down at the bottom as new rows are added...

    Tuesday, July 31, 2012 3:14 AM
  • I am quite certain that this actually is the last row:

    dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);

    Is that correct?

    Tuesday, July 31, 2012 3:22 AM
  • Filling them does not seem to have an effect in removal of dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1); either like:

            private void button1_Click(object sender, EventArgs e)
            {
                string wholeTimeValue;
                wholeTimeValue = dateTimePicker1.Value.ToString("HH:mm:tt");
    
                dataGridView1.Rows.Add(wholeTimeValue);
                dataGridView1.Rows[dataGridView1.Rows.Count - 2].Selected = true;
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = "test";
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].Value = 1;
       
                dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
            }

    So still there is this thing about an "uncommitted new row"? I'm not sure what to do yet...

    Tuesday, July 31, 2012 4:08 AM
  • I suppose I should mark the right answer and start a new thread as the question has kind of grown from the original...
    Tuesday, July 31, 2012 4:30 AM
  • The whole problem seems to lie in the fact that the DataGridView AllowUserToAddRows property was set to true so then the it makes sense that the last row cannot be deleted for if it were then how would the user add the new row? I get it. Good stuff.
    Tuesday, July 31, 2012 5:18 AM
  • Hi Sepoto,

      Welcome to MSDN Forum Support.

      I am glad to hear that you have solved the problem.Meanwhile,thank you for sharing the solution with us.

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, August 01, 2012 8:59 AM
    Moderator