BindingSource.Find 键比较不区分大小写吗?

发布于 2024-08-29 11:41:30 字数 378 浏览 9 评论 0原文

我正在使用 BindingSource.Find() 在刷新 DataGridView 后返回到用户的位置。我使用 BindingSource.Find() 和 RowID 作为我正在搜索的 DataColumn。不幸的是,Oracle 可以返回两个仅大小写不同的 RowID。

BindingSource.Find() 返回第一个匹配项,无论大小写。

查看MSDN文档:

public int Find(string propertyName, Object key)

它说propertyName比较不区分大小写,但没有提到key比较是否不区分大小写。

有谁知道如何使 BindingSource.Find 区分大小写?

I'm using BindingSource.Find() to return to the user's position after refreshing a DataGridView. I use BindingSource.Find() and a RowID as the DataColumn I'm searching on. Unfortunately, Oracle can return two RowIDs that differ only in their case.

BindingSource.Find() returns the first match regardless of case.

Looking at the MSDN docs:

public int Find(string propertyName, Object key)

it says that the propertyName comparison is case-insensitive, but does not mention whether the key comparison is.

Does anyone know how to make BindingSource.Find case sensitive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2024-09-05 11:41:30

对于DataView,您需要做的就是将父DataTableCaseSensitive 属性设置为true。我只是使用以下代码快速构建了原型,并且工作正常:

public Form1()
{
    InitializeComponent();

    DataSet set1 = new DataSet();

    // Some xml data to populate the DataSet with.
    string testXml =
    "<?xml version='1.0' encoding='UTF-8'?>" +
    "<numbers>" +
    "<number><name>one</name></number>" +
    "<number><name>two</name></number>" +
    "<number><name>Two</name></number>" +
    "<number><name>three</name></number>" +
    "</numbers>";

    // Read the xml.
    StringReader reader = new StringReader(testXml);
    set1.ReadXml(reader);

    // Get a DataView of the table contained in the dataset.
    DataTableCollection tables = set1.Tables;
    // Set the CaseSensetive property
    tables[0].CaseSensitive = true;
    DataView view1 = new DataView(tables[0]);

    // Create a DataGridView control and add it to the form.            
    dataGridView1.AutoGenerateColumns = true;            

    // 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;

    // Set the Position property to the results of the Find method.
    int itemFound = source1.Find("name", "Two");
    source1.Position = itemFound;
}

对于其他类型的 IBindingList,正如文档所说,您需要一个实现区分大小写的 Find 的底层列表。为了完整起见,我在下面显示了执行此操作的代码:

public Form1()
{
    InitializeComponent();

    // This uses my CaseSensitiveBindingList which I have code for later
    BindingList<DGVItems> source = new CaseSensitiveBindingList<DGVItems>() 
        { 
            new DGVItems { Number = "one" },
            new DGVItems{Number = "two"},
            new DGVItems{Number = "Two"}
        };

    BindingSource bindingSource = new BindingSource();
    bindingSource.DataSource = source;

    dataGridView1.DataSource = bindingSource;

    var index = bindingSource.Find("Number", "Two");

    // Index is 2 (third item) as desired.
    MessageBox.Show(number.ToString());

}

public class DGVItems
{
    public string Number { get; set; }
}

CaseSensitiveBindingList 的代码是:

public class CaseInsensitiveBindingList<T> : BindingList<T>
{
    protected override int FindCore(PropertyDescriptor prop, object key)
    {
        string stringKey = key as string;            

        bool keyIsString = stringKey != null;

        for (int i = 0; i < Count; ++i)
        {
            if (keyIsString && prop.PropertyType.IsAssignableFrom(typeof(string)))
            {
                if (stringKey.Equals(prop.GetValue(Items[i]).ToString(), StringComparison.CurrentCulture))
                    return i;
            }
            else
            {
                if (key.Equals(prop.GetValue(Items[i])))
                    return i;    
            }

        }

        return -1;
    }
}

该代码几乎肯定可以改进,但显示了一般概念。

For a DataView all you should need to do is set the CaseSensitive property of the parent DataTable to true. I just quickly prototyped that with the following code and it works fine:

public Form1()
{
    InitializeComponent();

    DataSet set1 = new DataSet();

    // Some xml data to populate the DataSet with.
    string testXml =
    "<?xml version='1.0' encoding='UTF-8'?>" +
    "<numbers>" +
    "<number><name>one</name></number>" +
    "<number><name>two</name></number>" +
    "<number><name>Two</name></number>" +
    "<number><name>three</name></number>" +
    "</numbers>";

    // Read the xml.
    StringReader reader = new StringReader(testXml);
    set1.ReadXml(reader);

    // Get a DataView of the table contained in the dataset.
    DataTableCollection tables = set1.Tables;
    // Set the CaseSensetive property
    tables[0].CaseSensitive = true;
    DataView view1 = new DataView(tables[0]);

    // Create a DataGridView control and add it to the form.            
    dataGridView1.AutoGenerateColumns = true;            

    // 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;

    // Set the Position property to the results of the Find method.
    int itemFound = source1.Find("name", "Two");
    source1.Position = itemFound;
}

For other types of IBindingList you need, as the docs say, an underlying list that implements a Find which is case sensitive. For completeness I have shown the code to do this below:

public Form1()
{
    InitializeComponent();

    // This uses my CaseSensitiveBindingList which I have code for later
    BindingList<DGVItems> source = new CaseSensitiveBindingList<DGVItems>() 
        { 
            new DGVItems { Number = "one" },
            new DGVItems{Number = "two"},
            new DGVItems{Number = "Two"}
        };

    BindingSource bindingSource = new BindingSource();
    bindingSource.DataSource = source;

    dataGridView1.DataSource = bindingSource;

    var index = bindingSource.Find("Number", "Two");

    // Index is 2 (third item) as desired.
    MessageBox.Show(number.ToString());

}

public class DGVItems
{
    public string Number { get; set; }
}

And the code for the CaseSensitiveBindingList is:

public class CaseInsensitiveBindingList<T> : BindingList<T>
{
    protected override int FindCore(PropertyDescriptor prop, object key)
    {
        string stringKey = key as string;            

        bool keyIsString = stringKey != null;

        for (int i = 0; i < Count; ++i)
        {
            if (keyIsString && prop.PropertyType.IsAssignableFrom(typeof(string)))
            {
                if (stringKey.Equals(prop.GetValue(Items[i]).ToString(), StringComparison.CurrentCulture))
                    return i;
            }
            else
            {
                if (key.Equals(prop.GetValue(Items[i])))
                    return i;    
            }

        }

        return -1;
    }
}

That code could almost certainly be improved but shows the general concept.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文