DataView Rowfilter 表达式 C#
DataView Rowfilter Expression C#
我有一个数据表,其中一列是特定类型的对象,比如 typeOf(A) - A 是我的自定义 class。 class A 具有文本 属性 和布尔等其他属性。从用户或外部代码中,我只获得要在 RowFilter 中用于过滤的文本值,在下面的示例中,我将获得 "cat" 作为我要使用的过滤器。使用这个如何创建 RowFilterExpression?
class A
{
public string Text{get;set;}
public bool isReadonly {get;set;}
}
DataTable table = new DataTable();
table.Columns.Add("col1", typeOf(A));
table.Rows.Add(new A{Text = "cat", isReadOnly = true,})
table.Rows.Add(new A{Text = "dog", isReadOnly = false,})
string filter = "cat";
DataView dataView = new DataView(table);
dataView.RowFilter = "[col1]='"+filter+"'";
上面的表达式不起作用,因为它试图将它与单元格中的对象匹配。如何使用此过滤器表达式单独过滤掉第一行?
感谢任何帮助。
尝试覆盖对象中的 ToString
方法,然后您当前的过滤代码应该可以工作
class A
{
public string Text{get;set;}
public bool isReadonly {get;set;}
public override string ToString()
{
return this.Text;
}
}
像这样覆盖对象 ToString 方法:
class A
{
public string Text { get; set; }
public bool isReadonly { get; set; }
public override string ToString()
{
return this.Text;
}
}
你可以使用:
dataView.RowFilter = string.Format("Convert([col1], 'System.String') = '{0}'", filter);
我有一个数据表,其中一列是特定类型的对象,比如 typeOf(A) - A 是我的自定义 class。 class A 具有文本 属性 和布尔等其他属性。从用户或外部代码中,我只获得要在 RowFilter 中用于过滤的文本值,在下面的示例中,我将获得 "cat" 作为我要使用的过滤器。使用这个如何创建 RowFilterExpression?
class A
{
public string Text{get;set;}
public bool isReadonly {get;set;}
}
DataTable table = new DataTable();
table.Columns.Add("col1", typeOf(A));
table.Rows.Add(new A{Text = "cat", isReadOnly = true,})
table.Rows.Add(new A{Text = "dog", isReadOnly = false,})
string filter = "cat";
DataView dataView = new DataView(table);
dataView.RowFilter = "[col1]='"+filter+"'";
上面的表达式不起作用,因为它试图将它与单元格中的对象匹配。如何使用此过滤器表达式单独过滤掉第一行?
感谢任何帮助。
尝试覆盖对象中的 ToString
方法,然后您当前的过滤代码应该可以工作
class A
{
public string Text{get;set;}
public bool isReadonly {get;set;}
public override string ToString()
{
return this.Text;
}
}
像这样覆盖对象 ToString 方法:
class A
{
public string Text { get; set; }
public bool isReadonly { get; set; }
public override string ToString()
{
return this.Text;
}
}
你可以使用:
dataView.RowFilter = string.Format("Convert([col1], 'System.String') = '{0}'", filter);