軟件開發技術交流,同學習共進步,歡迎加群, 群號:169600532
最近C/S項目中用到FarPoint.Win.Spread,想在表頭加個全選的checkbox,實現效果如圖:
列的設置大家都清楚,直接可視化視圖中設置該列CellType為CheckBox類型即可
表頭的設置就需要費些周折了。一般是在加載時code,代碼:

1 //設置行表頭 2 FarPoint.Win.Spread.CellType.CheckBoxCellType celltype = new FarPoint.Win.Spread.CellType.CheckBoxCellType(); 3 celltype.Caption = "全選"; 4 spdPrintInfo_Sheet1.ColumnHeader.Cells[0, 0].CellType = celltype; 5 spdPrintInfo_Sheet1.ColumnHeader.Cells[0, 0].Text = "False";
由於我要把全選放在第一行,所以固定為【0,0】
然后需要給這個CheckBox定義點擊事件,一般事件就是該控件的CellClick事件,詳細代碼如下:

1 private void spdPrintInfo_CellClick(object sender, CellClickEventArgs e) 2 { 3 if (e.Column == 0) 4 { 5 if (e.ColumnHeader) 6 { 7 if (spdPrintInfo_Sheet1.ColumnHeader.Cells[0, 0].Text == "False") 8 { 9 spdPrintInfo_Sheet1.ColumnHeader.Cells[0, 0].Text = "True"; 10 int rowcount = spdPrintInfo_Sheet1.RowCount; 11 for (int i = 0; i < rowcount; i++) 12 { 13 spdPrintInfo_Sheet1.Cells[i, 0].Text = "True"; 14 } 15 } 16 else 17 { 18 spdPrintInfo_Sheet1.ColumnHeader.Cells[0, 0].Text = "False"; 19 int rowcount = spdPrintInfo_Sheet1.RowCount; 20 for (int i = 0; i < rowcount; i++) 21 { 22 spdPrintInfo_Sheet1.Cells[i, 0].Text = "False"; 23 } 24 } 25 } 26 else 27 { 28 if (spdPrintInfo_Sheet1.Cells[e.Row, 0].Text == "False") 29 { 30 bool allCheck = true; 31 spdPrintInfo_Sheet1.Cells[e.Row, 0].Text = "True"; 32 for (int i = 0; i < spdPrintInfo_Sheet1.RowCount; i++) 33 { 34 if (spdPrintInfo_Sheet1.Cells[i, 0].Text == "False") 35 { 36 allCheck = false; 37 break; 38 } 39 } 40 if (allCheck) 41 { 42 spdPrintInfo_Sheet1.ColumnHeader.Cells[0, 0].Text = "True"; 43 } 44 } 45 else 46 { 47 spdPrintInfo_Sheet1.Cells[e.Row, 0].Text = "False"; 48 //將表頭也設置為不勾選 49 spdPrintInfo_Sheet1.ColumnHeader.Cells[0, 0].Text = "False"; 50 } 51 } 52 } 53 }
如此,自己coding的全選就完成了,方法不是最好的,歡迎大家指正或建議更高效、簡單的方法。