|
■No99882 (魔界の仮面弁士) に追記 > SQL Server で NULL を末尾にしてソートする場合は > ORDER BY num ASC > の代わりに > ORDER BY ISNULL(num,0x7fffffff) ASC > のようにします。
ISNULL よりも、CASE を使った方が良いですね。
-- ORDER BY num ASC NULLS LAST の代用 ORDER BY CASE WHEN num IS NULL THEN 0 ELSE 1 END ASC, num ASC
-- ORDER BY num DESC NULLS LAST の代用 ORDER BY CASE WHEN num IS NULL THEN 0 ELSE 1 END ASC, num DESC
-- ORDER BY num ASC NULLS FIRST の代用 ORDER BY CASE WHEN num IS NULL THEN 0 ELSE 1 END DESC, num ASC
-- ORDER BY num DESC NULLS FIRST の代用 ORDER BY CASE WHEN num IS NULL THEN 0 ELSE 1 END DESC, num DESC
ということで、DataGridView で対応する場合も同じ方式に差し換えてみる。
> dataGridView1.AutoGenerateColumns = false; > tbl.Columns.Add("numASC", typeof(int), "ISNULL([num], " + int.MaxValue + ")"); > tbl.Columns.Add("numDESC", typeof(int), "ISNULL([num], " + int.MinValue + ")"); dataGridView1.AutoGenerateColumns = false; tbl.Columns.Add("numIsNull", typeof(bool), "[num] IS NULL");
> if (header.SortGlyphDirection == SortOrder.Ascending) > { > view.Sort = "[numDESC] DESC"; > header.SortGlyphDirection = SortOrder.Descending; > } > else > { > view.Sort = "[numASC] ASC"; > header.SortGlyphDirection = SortOrder.Ascending; > } if (header.SortGlyphDirection == SortOrder.Ascending) { view.Sort = "[numIsNull] ASC, [num] DESC"; header.SortGlyphDirection = SortOrder.Descending; } else { view.Sort = "[numIsNull] ASC, [num] ASC"; header.SortGlyphDirection = SortOrder.Ascending; }
|