开发者

BindingSource.Find key comparison is case insensitive?

开发者 https://www.devze.com 2022-12-27 16:57 出处:网络
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.Unfortuna开发者_StackOverfl

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. Unfortuna开发者_StackOverflowtely, 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消