2

DataGridView を更新した後、BindingSource.Find() を使用してユーザーの位置に戻ります。検索対象の DataColumn として BindingSource.Find() と RowID を使用します。残念ながら、Oracle は大文字と小文字のみが異なる 2 つの RowID を返すことがあります。

BindingSource.Find() は、大文字と小文字に関係なく最初の一致を返します。

MSDNのドキュメントを見る:

public int Find(string propertyName, Object key)

propertyName の比較では大文字と小文字が区別されないと書かれていますが、キーの比較では大文字と小文字が区別されるかどうかについては言及されていません。

BindingSource.Find の大文字と小文字を区別する方法を知っている人はいますか?

4

1 に答える 1

1

親のプロパティをtrueにDataView設定するだけです。次のコードですぐにプロトタイプを作成したところ、正常に動作しました。CaseSensitiveDataTable

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

そのコードはほぼ確実に改善される可能性がありますが、一般的な概念を示しています。

于 2011-04-09T17:20:41.837 に答える