2017-05-14 137 views
-5

过滤数据表列为了给null或空白过滤数据表列我用这个过滤器,但它不到风度工作如何为空或空白

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

为什么不是dt = dt.AsEnumerable()。其中​​(x => x.Field [0]!= null).CopyToDataTable(); – jdweng

+0

sory it dosn't work我需要编辑上面的过滤器来过滤列或名称为空的空白dt.Columns [int.Parse(comboBox8.Text) - 1 – alhussein

+0

您可以使用列名而不是索引:dt = dt .AsEnumerable()。Where(x =>(x.Field [comboBox8.Text]!= null) ||!string.IsNullOrWhiteSpace(x.Field ([comboBox8.Text]))。CopyToDataTable(); – jdweng

回答

0

我通过解决这个问题: 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; 
    } 

然后我使用特定列这个代码在数据网格视图: 如果((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”; (“[{0}]不是LIKE'%{1}%'”,dt.Columns [int.Parse(comboBox8.Text))。 - 1] .ColumnName,“Empty”);