【问题标题】:How to filter data table column for null or white space如何过滤数据表列的空值或空格
【发布时间】:2017-10-13 12:24:55
【问题描述】:

为了过滤空或空白的数据表列,我使用了这个过滤器,但它不起作用

dt.DefaultView.RowFilter = 
string.Format("[{0}] ='%{1}%'(string.IsNullOrWhiteSpace(dt.Columns[int.Parse(comboBox8.Text) - 1].ColumnName.ToString())),true);

【问题讨论】:

  • 为什么不 dt = dt.AsEnumerable().Where(x => x.Field[0] != null).CopyToDataTable();
  • 所以它不起作用我需要编辑上面的过滤器来过滤列名 dt.Columns[int.Parse(comboBox8.Text) - 1 的空值或空格
  • 您可以使用列名代替索引:dt = dt.AsEnumerable().Where(x => (x.Field[comboBox8.Text] != null) || !string .IsNullOrWhiteSpace(x.Field([comboBox8.Text])).CopyToDataTable();
  • 对不起,它不起作用

标签: c# filter datatable


【解决方案1】:

我通过以下方式解决它: 1- 利用这个乐趣

public static bool IsNullOrWhiteSpace(string value)
    {
        if (value != null)
        {
            for (int i = 0; i < value.Length; i++)
            {
                if (!char.IsWhiteSpace(value[i]))
                {
                    return false;
                }
            }
        }
        return true;
    }

然后我将此代码用于数据网格视图中的特定列: if((function.IsNullOrWhiteSpace(dataGridView3.Rows[j].Cells[int.Parse(comboBox8.Text) - 1].Value.ToString()) == true)) dataGridView3.Rows[j].Cells[int.Parse(comboBox8.Text) - 1].Value = "Empty";

然后我使用这个过滤器: dt.DefaultView.RowFilter = string.Format("[{0}] not LIKE '%{1}%'", dt.Columns[int.Parse(comboBox8.Text) - 1].ColumnName, "Empty");

【讨论】:

    猜你喜欢
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2015-11-08
    • 2022-01-04
    相关资源
    最近更新 更多