WinFormsのDataGridViewでソートの定義をカスタマイズしたいのですが、どうもうまくいきません。
DataSourceはバインドしていません。
カラムは複数存在しますが、ソートはカラムヘッダーでクリックしたその1列だけソートできれば、他の列はどうなっていても構いません。
列1
ーーーーー
固定
ーーーーー
10
ーーーーー
100
ーーーーー
null
ーーーーー
null
ーーーーー
列1(カラム)のタブをクリックするとソートが行われますが、私の期待値としては
列1
ーーーーー
固定
ーーーーー
100
ーーーーー
10
ーーーーー
null
ーーーーー
null
ーーーーー
になってほしいです。
条件としては
・1行目は何があっても常に一番上。
・nullのセルは必ず下方へ。
つまり、数値が入っているセルだけが昇降される。
たくさん試しましたが完全に行き詰ったのでご教授していただけると幸いです。
よろしくお願いいたします。
C#
1public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 dataGridView1.Rows.Add("固定"); 7 dataGridView1.Rows.Add("10"); 8 dataGridView1.Rows.Add("100"); 9 dataGridView1.Rows.Add(); 10 dataGridView1.Rows.Add(); 11 12 } 13 private void dataGridView1_SortCompare_1(object sender, DataGridViewSortCompareEventArgs e) 14 { 15 if (e.CellValue1 == null || e.CellValue2 == null) 16 { 17 e.SortResult = 0; 18 } 19 20 else if(e.CellValue1.ToString() == "固定" || e.CellValue2.ToString() == "固定") 21 { 22 e.SortResult = 0; 23 } 24 else 25 { 26 int a = Convert.ToInt32(e.CellValue1); 27 int b = Convert.ToInt32(e.CellValue2); 28 int sum = a - b; 29 if (sum < 0) 30 { 31 e.SortResult = 1; 32 } 33 else if (sum > 0) 34 { 35 e.SortResult = -1; 36 } 37 else if (sum == 0) 38 { 39 e.SortResult = 0; 40 } 41 } 42 43 e.Handled = true; 44 } 45 } 46
この質問の投稿者はすでに退会しているため、質問者からの返信がない点にご留意ください。
回答1件
この質問の投稿者はすでに退会しているため、質問者からの返信やベストアンサーの選定がない点にご留意ください。
あなたの回答
tips
プレビュー