none
BindingSource.Filter 中的"[" RRS feed

  • 问题

  • 怎么在BindingSource的Filter中筛选带"["和"]"的字符串,例如bs.filter = "like ab[cde]fg%"

    会出异常,like运算符中出错:字符串模式"ab[cde]fg%"无效

    请问这个问题怎么解决,不能将筛选字符串中的"["和"]"去掉,而是要让BindingSource知道"["和"]"是筛选字符串的一部分,没有特殊意义


    2016年9月17日 18:24

答案

  • 你好,

    根据下面的文档,我们知道,这个特殊字符,需要用[] 包住它,象这样:

    source1.Filter = "artist like 'ab[[]cde[]]fg%' OR cd = 'Tigerlily'"

    下面是一个完整的例子:

      DataSet set1 = new DataSet();
    
                // Some xml data to populate the DataSet with.
                string musicXml =
                    "<?xml version='1.0' encoding='UTF-8'?>" +
                    "<music>" +
                    "<recording><artist>Coldplay</artist><cd>X&amp;Y</cd></recording>" +
                    "<recording><artist>ab[cde]fgMatthews</artist><cd>Under the Table and Dreaming</cd></recording>" +
                    "<recording><artist>ab[cde]fgMatthews</artist><cd>Live at Red Rocks</cd></recording>" +
                    "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" +
                    "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" +
                    "</music>";
    
                // Read the xml.
                StringReader reader = new StringReader(musicXml);
                set1.ReadXml(reader);
    
                // Get a DataView of the table contained in the dataset.
                DataTableCollection tables = set1.Tables;
                DataView view1 = new DataView(tables[0]);
    
                // Create a DataGridView control and add it to the form.
                DataGridView datagridview1 = new DataGridView();
                datagridview1.AutoGenerateColumns = true;
                this.Controls.Add(datagridview1);
    
                // Create a BindingSource and set its DataSource property to
                // the DataView.
                BindingSource source1 = new BindingSource();
                source1.DataSource = view1;
    
                // Set the data source for the DataGridView.
                datagridview1.DataSource = source1;
    
                //The Filter string can include Boolean expressions.
                source1.Filter = "artist like 'ab[[]cde[]]fg%' OR cd = 'Tigerlily'";

    https://msdn.microsoft.com/zh-cn/library/system.data.datacolumn.expression.aspx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年9月20日 6:36
    版主

全部回复

  • 不允许这样做,参考这里,筛选字符串只能出现在字符串的开头和结尾,中间部分不允许出现。

    考虑在数据库层做筛选或是用一个DataView过滤

    http://stackoverflow.com/questions/4717923/net-filter-bindingsource

    1down voteaccepted

    Per the documentation at http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

    Wildcard Characters Both the * and %  can be used interchangeably for  wildcard characters in a LIKE  comparison. If the string in a LIKE  clause contains a * or %, those  characters should be enclosed in  brackets ([]). If a bracket is in the  clause, each bracket character should  be enclosed in brackets (for example  [[] or []]). A wildcard is allowed at  the start and end of a pattern, or at  the end of a pattern, or at the start  of a pattern. For example:

    "ItemName LIKE 'product'"

    "ItemName LIKE '*product'"

    "ItemName LIKE 'product*'"

    Wildcard characters are not allowed in  the middle of a string. For example,  'te*xt' is not allowed.

    Therefore, you can't use LIKE to do what you want.  Your next best bet is to get the string representation and use a Regex to parse it looking for the text you want.  That's going to be SLOW.

    You COULD do

    column1 LIKE '%John%' AND LIKE %Doe%'

    but the logic is different and may not be what you really want.

    Edit - added

    You might be better off doing the filtering at the server level, as your DB might support a wildcard in the middle of the string.  I just tried it on our SQL Server and it works fine. (SQL Server 2005)


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年9月18日 0:02
  • 试试转义

    \[

    \]


    http://feiyun0112.cnblogs.com/


    2016年9月18日 0:58
    版主
  • 你好,

    根据下面的文档,我们知道,这个特殊字符,需要用[] 包住它,象这样:

    source1.Filter = "artist like 'ab[[]cde[]]fg%' OR cd = 'Tigerlily'"

    下面是一个完整的例子:

      DataSet set1 = new DataSet();
    
                // Some xml data to populate the DataSet with.
                string musicXml =
                    "<?xml version='1.0' encoding='UTF-8'?>" +
                    "<music>" +
                    "<recording><artist>Coldplay</artist><cd>X&amp;Y</cd></recording>" +
                    "<recording><artist>ab[cde]fgMatthews</artist><cd>Under the Table and Dreaming</cd></recording>" +
                    "<recording><artist>ab[cde]fgMatthews</artist><cd>Live at Red Rocks</cd></recording>" +
                    "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" +
                    "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" +
                    "</music>";
    
                // Read the xml.
                StringReader reader = new StringReader(musicXml);
                set1.ReadXml(reader);
    
                // Get a DataView of the table contained in the dataset.
                DataTableCollection tables = set1.Tables;
                DataView view1 = new DataView(tables[0]);
    
                // Create a DataGridView control and add it to the form.
                DataGridView datagridview1 = new DataGridView();
                datagridview1.AutoGenerateColumns = true;
                this.Controls.Add(datagridview1);
    
                // Create a BindingSource and set its DataSource property to
                // the DataView.
                BindingSource source1 = new BindingSource();
                source1.DataSource = view1;
    
                // Set the data source for the DataGridView.
                datagridview1.DataSource = source1;
    
                //The Filter string can include Boolean expressions.
                source1.Filter = "artist like 'ab[[]cde[]]fg%' OR cd = 'Tigerlily'";

    https://msdn.microsoft.com/zh-cn/library/system.data.datacolumn.expression.aspx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年9月20日 6:36
    版主
  •  like 'ab[[]cde[]]fg%'  可用,多谢!多谢!
    2016年9月20日 20:32