【问题标题】:dataview rowfilter LIKE * works not exactlydataview rowfilter LIKE * 工作不完全
【发布时间】:2016-06-24 03:24:46
【问题描述】:

我的dataview 有 1 列名为 RecordType(SQL 中的主键,键入 varchar(18)),其中有 3 条记录 {"NH", "NTH", "XH"}。

现在,我想通过编码过滤以“N”开头的数据:

( myDataView.RowFilter = "RecordType LIKE 'N*'"),但只返回了 1 条记录 ({"NTH"})

确切的结果必须有 2 条记录 ({"NH", "NTH"})。

请问我该如何纠正这个问题?

【问题讨论】:

  • 试试myDataView.RowFilter = "RecordType LIKE 'N*'"
  • 您如何检索记录?也许你有一个错误,只是检索第一行。
  • 嗨 Eric J!"你检索记录的情况如何"
  • 感谢 Jim Hewitt,您的代码是正确的,但结果是错误的......

标签: c#


【解决方案1】:

我试过了,效果很好:

myDataView.RowFilter = "RecordType LIKE 'N*'";

它将返回“NH”和“NTH”。

【讨论】:

  • 如果不看他的更多代码,有点难以判断这是否是解决方案。
【解决方案2】:
      private void button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN");
        DataView myDataView = new DataView();
        DataTable myDataTable = new DataTable();
        myDataTable.Columns.Add("RecordType");
        myDataTable.PrimaryKey = new DataColumn[] { myDataTable.Columns["RecordType"] };
        DataRow dr = myDataTable.NewRow();
        dr["RecordType"] = "NH";
        myDataTable.Rows.Add(dr);
        dr = myDataTable.NewRow();
        dr["RecordType"] = "NTH";
        myDataTable.Rows.Add(dr);
        dr = myDataTable.NewRow();
        dr["RecordType"] = "XH";
        myDataTable.Rows.Add(dr);
        myDataView = myDataTable.DefaultView;
        myDataView.Table.Locale = new CultureInfo("en-US");// Without this line, the filter on dataview not exactly always!!!
        myDataView.RowFilter = "RecordType LIKE 'N*'";

        for (int i = 0; i < myDataView.Count; i++)
            MessageBox.Show(myDataView[i]["RecordType"].ToString());
    }

非常感谢大家的宝贵时间。我发现,当我们设置系统的CurrentCulture时,它会影响dataview的过滤和排序。

为了解决这个问题,我们必须重新分配dataview的locate默认值:

myDataView.Table.Locale = new CultureInfo("en-US");

我想是的。还有什么好主意吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多