SortedDictionaryって?
SortedDirectory(Of TKey, TValue)
で、TKeyの昇順に並び替えてくれるっぽい。
DataGridViewの行を複数選択した時、Gridの先頭から順番(昇順)にデータ(SeqNo)を取得したいときに使ってみた。
実装してみた。
sample1.vb
Dim sortedDictionary As New SortedDictionary(Of Integer, Integer)
For Each dataGridViewRow As DataGridViewRow In DataGridView1.SelectedRows
sortedDictionary.Add(dataGridViewRow.Index, dataGridViewRow.Cells("SeqNo").Value)
Next
sample.cs
var sortedDictionary = new SortedDictionary<int, int>();
foreach (DataGridViewRow dataGridViewRow in dataGridView1.SelectedRows)
{
sortedDictionary.Add(dataGridViewRow.Index, Convert.ToInt32(dataGridViewRow.Cells["SqlNo"].Value));
}
値の取り出し方はこんな感じ。
sample2.vb
For Each kvp As KeyValuePair(Of Integer, Integer) In sortedDictionary
Console.WriteLine(kvp.Value)
Next
sample2.cs
foreach (KeyValuePair<int, int> kvp in sortedDictionary)
{
Console.WriteLine(kvp.Value);
}
↧