【问题标题】:Trim Blank Space in DataGridView Cell修剪 DataGridView 单元格中的空白区域
【发布时间】:2018-01-03 14:49:12
【问题描述】:

情况/问题

我创建了一个 DataGridView1,然后填充了一个列表。每当我单击一个单元格时,它应该引用它并在编辑时单元格不包含值时抛出异常。不幸的是,一些单元格在开头创建了一个空格(“”),我希望能够(在离开单元格时)在它为空和/或它为“”之间进行过滤。

知识

我知道 Trim() 会删除所有尾随空格和后续空格(“###”->“###”),只是似乎不明白修剪了空格的单元格如何不平等的 ””。不幸的是,我不能使用 .Length() 因为单元格值也大多是 1 位整数。

当前代码

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
   }

最大的问题是每当单元格中留下一个单数空格时,因为当我尝试使用 .ToString() 时,它会引发错误“输入字符串的格式不正确”。

提前致谢

【问题讨论】:

    标签: c# winforms visual-studio datagridview trim


    【解决方案1】:

    我无法复制您的问题。虽然这可行并且它可以处理单个空格以及空字段。

    你发布的代码和我的主要区别在于这部分:

    你的

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

    我的

    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; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-21
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      相关资源
      最近更新 更多