Search code examples
c#winformsvisual-studiodatagridviewtrim

Trim Blank Space in DataGridView Cell


SITUATION/PROBLEM

I have a DataGridView1 that is created and then populated with a list. Whenever I click on a cell, it should reference it and throw an exception if the cell does not contain a value when edited. Unfortunately, some cells were created with a space (" ") at the beginning and I want to be able to (upon leaving the cell) filter between it being null and/or it being " ".

KNOWLEDGE

I understand that Trim() removes all trailing white spaces and proceeding spaces (" ### " -> "###") and just don't seem to understand how a cell with a space in it trimmed does not equal "". Unfortunately, I can't use .Length() because the cell values are mostly 1 digit integers as well.

CURRENT CODE

foreach (DataGridViewRow row in dataGridView1.Rows)
{
   if (row.Cells[0].Value.ToString() != null && row.Cells[0].Value.ToString().Trim() != "")
   {
      //it always hits here and passes through the conditional statement
      //contains method to set textbox equal to cell value
   }

The biggest issue is whenever a singular space is left in a cell because when I try to use .ToString() it throws an error "Input string was not in a correct format".

Thanks in advance


Solution

  • I was unable to replicate your problem. Though this works and it handles single spaces, as well as empty fields.

    The main difference between your posted code and mine is this part:

    Yours

    if (row.Cells[0].Value.ToString() != null && row.Cells[0].Value.ToString().Trim() != "")

    Mine

    if (row.Cells[0].Value != null && row.Cells[0].Value.ToString().Trim() != "")

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace TrimBlanksInDGV_45358146
    {
        public partial class Form1 : Form
        {
            public static string whatwasit = "";
            public Form1()
            {
    
                InitializeComponent();
                List<listitem> datasource = new List<listitem>();
                datasource.Add(new TrimBlanksInDGV_45358146.listitem { name = "name1", last = "last1" });
                datasource.Add(new TrimBlanksInDGV_45358146.listitem { name = "name2", last = "last2" });
                datasource.Add(new TrimBlanksInDGV_45358146.listitem { name = "name3", last = "last3" });
                datasource.Add(new TrimBlanksInDGV_45358146.listitem { name = "name4", last = "last4" });
                datasource.Add(new TrimBlanksInDGV_45358146.listitem { name = " ", last = "last5" });
                datasource.Add(new TrimBlanksInDGV_45358146.listitem { name = "", last = "last6" });
                dataGridView1.DataSource = datasource;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[0].Value != null && row.Cells[0].Value.ToString().Trim() != "")
                    {
                        whatwasit = "not empty or \"\"";
                    }
                    else
                    {
                        whatwasit = "empty or \"\"";
                    }
                }
            }
        }
    
        public class listitem
        {
            public string name { get; set; }
            public string last { get; set; }
        }
    }