1 取得當前行號、列號。
int row=e.Row; int count=e.Count; 或者: int rowindex = fpSpread1.ActiveSheet.ActiveRowIndex; int columnindex = fpSpread1.ActiveSheet.ActiveColumnIndex; |
2 單擊一行變顏色。
private void spdResult_CellClick(object sender, FarPoint.Win.Spread.CellClickEventArgs e) { //單擊Spread列頭時,什么也不處理 if(!e.ColumnHeader) { if(spdResult.Sheets[0].Rows.Count!=0) { for(int i=0;i<spdResult.Sheets[0].Rows.Count;i++) { spdResult.Sheets[0].Rows[i].BackColor=System.Drawing.Color.White; } int row=e.Row; spdResult.Sheets[0].Rows[row].BackColor=System.Drawing.Color.FromArgb(((System.Byte)(192)),((System.Byte)(255)), ((System.Byte)(255))); } } } |
3 將Spread的單元格內容付值給一控件的Text
txtItemCD.Text = spdResult.Sheets[0].Cells[row,count].Text; |
4 給Spread的指定單元格付值。
spdResult.Sheets[0].Cells[row,count].Text = txtItemCD.Text; |
5 通過上下光標鍵改變選中行顏色
private void spdResult_LeaveCell(object sender, FarPoint.Win.Spread.LeaveCellEventArgs e) { //首先檢查spread行數是否為0 if(spdResult.Sheets[0].Rows.Count==0) { return; } else { for(int i=0;i<spdResult.Sheets[0].Rows.Count;i++) { spdResult.Sheets[0].Rows[i].BackColor=System.Drawing.Color.White; } int row=e.NewRow; spdResult.Sheets[0].Rows[row].BackColor=System.Drawing.Color.FromArgb(((System.Byte)(192)),((System.Byte)(255)), ((System.Byte)(255))); } } |
6 下拉列表加載數據(ComBobox)
・ 列表追加(適合於數據量少的情況)
FarPoint.Win.Spread.CellType.ComboBoxCellType cb4 = newFarPoint.Win.Spread.CellType.ComboBoxCellType(); cb4.ListWidth = 96; cb4.Editable = true; cb4.MaxDrop = 10; cb4.MaxLength = 1; string[] priceTagList = new string[]{" 0 無"," 1 有"}; cb4.Items = priceTagList; this.spdSetList.ActiveSheet.Columns[4].CellType = cb4; |
・ 從數據庫追加
FarPoint.Win.Spread.CellType.ComboBoxCellType cb12 = newFarPoint.Win.Spread.CellType.ComboBoxCellType(); cb12.ListWidth = 150; cb12.Editable = true; cb12.MaxDrop = 10; cb12.MaxLength = 8; //dsEmployee:數據集Dataset,已經加載好數據的Dataset string[] employeeList = DataSetToArray(dsEmployee, 8); cb12.Items = employeeList; this.spdSetList.ActiveSheet.Columns[12].CellType = cb12;
private string[] DataSetToArray(DataSet ds, int BlankNum) { int i = 0; int NumLength = 0; string[] returnArray = new string[ds.Tables[0].Rows.Count];
DataRow foundRows = ds.Tables[0].Rows[ds.Tables[0].Rows.Count -1]; NumLength = foundRows[0].ToString().Length;
foreach(DataRow dr in ds.Tables[0].Rows) { returnArray[i] = dr[0].ToString().PadLeft(BlankNum, ' ') + " " + dr[1].ToString(); i++; } return returnArray; } |
7 Focus移動(跨列)
public frmProdSetDetail() { InitializeComponent(); IsMod = flag;
FarPoint.Win.Spread.InputMap im; im = spdResult.GetInputMap(InputMapMode.WhenFocused); im.Put(new Keystroke(Keys.Enter,Keys.None),SpreadActions.MoveToNextColumnWrap); im.Put(new Keystroke(Keys.Tab,Keys.None),SpreadActions.MoveToNextColumnWrap);
im = spdResult.GetInputMap(InputMapMode.WhenAncestorOfFocused); im.Put(new Keystroke(Keys.Enter,Keys.None),SpreadActions.MoveToNextColumnWrap); im.Put(new Keystroke(Keys.Tab,Keys.None),SpreadActions.MoveToNextColumnWrap); } |
指定單元格獲得焦點
this.fpSpread1.ActiveSheet.SetActiveCell(row,column); |
8 事件觸發順序
_Enter _EnterCell _EditModeOn _EditChange _EditModeOff _LeaveCell
9 用隱藏列保存原始數據
10 設定列類型
private void SpreadSetting() { string[] ProductHandleTypeList = new string[]{" ","1 販売/仕入","2 販売","3 仕入"}; FarPoint.Win.Spread.CellType.ComboBoxCellType cb3 = newFarPoint.Win.Spread.CellType.ComboBoxCellType(); cb3.ListWidth = 100; cb3.Editable = true; cb3.MaxDrop = 5; cb3.MaxLength = 1; cb3.Items = ProductHandleTypeList;
this.spdResult.ActiveSheet.Columns[5].CellType = cb3; //設置一般數據型
FarPoint.Win.Spread.CellType.NumberCellType nmbrcell = newFarPoint.Win.Spread.CellType.NumberCellType(); nmbrcell.ShowSeparator = false; nmbrcell.DecimalPlaces = 0; nmbrcell.LeadingZero = FarPoint.Win.Spread.CellType.LeadingZero.UseRegional; nmbrcell.MaximumValue = 9999; nmbrcell.MinimumValue = 1; this.spdResult.ActiveSheet.Columns[13].CellType = nmbrcell;
//設置JAN
FarPoint.Win.Spread.CellType.NumberCellType nmbrcellJan = newFarPoint.Win.Spread.CellType.NumberCellType(); nmbrcellJan.ShowSeparator = false; nmbrcellJan.DecimalPlaces = 0; nmbrcellJan.LeadingZero = FarPoint.Win.Spread.CellType.LeadingZero.UseRegional; nmbrcellJan.MaximumValue = 9999999999999; nmbrcellJan.MinimumValue = 0; this.fpSpread1.ActiveSheet.Columns[0].CellType = nmbrcellJan; this.fpSpread1.ActiveSheet.Columns[0].HorizontalAlignment =FarPoint.Win.Spread.CellHorizontalAlignment.Left;
//
FarPoint.Win.Spread.CellType.NumberCellType numberCellType1 = newFarPoint.Win.Spread.CellType.NumberCellType(); numberCellType1.ShowSeparator = true; numberCellType1.DecimalPlaces = 0; numberCellType1.LeadingZero = FarPoint.Win.Spread.CellType.LeadingZero.UseRegional; numberCellType1.MaximumValue = 99999999; numberCellType1.MinimumValue = 0; this.spdResult.ActiveSheet.Columns[20].CellType = numberCellType1;
//%數的設定
FarPoint.Win.Spread.CellType.PercentCellType prctcell = newFarPoint.Win.Spread.CellType.PercentCellType(); prctcell.PercentSign = "%"; this.spdResult.ActiveSheet.Columns[33].CellType = prctcell; //日期的設定
FarPoint.Win.Spread.CellType.DateTimeCellType datecell = newFarPoint.Win.Spread.CellType.DateTimeCellType(); datecell.MaximumDate = new System.DateTime(2050, 12, 31, 0, 0, 0, 0); datecell.MinimumDate = new System.DateTime(2001, 1, 1, 0, 0, 0, 0);
this.spdResult.ActiveSheet.Columns[27,30].CellType = datecell; } |
11 列、單元格鎖定
//鎖定
this.fpSpread1.ActiveSheet.Columns[0,4].Locked = true;//鎖定列范圍
this.fpSpread1.ActiveSheet.Columns[0].Locked = true;//鎖定單列
//解鎖
this.fpSpread1.ActiveSheet.Columns[0,4].Locked = false;//解鎖列范圍
this.fpSpread1.ActiveSheet.Columns[0].Locked = false; |
12 Spread追加行、列
//追加行
int rowindex = this.fpSpread1.ActiveSheet.Rows.Count; this.fpSpread1.ActiveSheet.Rows.Add(rowindex,1);
//追加列
int columnindex = this.fpSpread1.ActiveSheet.Columns.Count; this.fpSpread1.ActiveSheet.Columns.Add(columnindex,1); |
13 行、列刪除
//刪除行
this.fpSpread1.ActiveSheet.Rows.Remove(startindex,count);
//追加列
this.fpSpread1.ActiveSheet.Columns.Remove(startindex,count); |
14 button事件
private void spdSetList_ButtonClicked(object sender,FarPoint.Win.Spread.EditorNotifyEventArgs e) { int rowCount = e.Row; int columnCount = e.Column;
if (e.Column == 16) { if (message.ShowMessage("ConfirmDelete", "選択したライン") == DialogResult.Yes) { string DeleteTag = this.spdSetList.ActiveSheet.Cells[rowCount,columnCount+1].Text.Trim(); if (DeleteTag == "0" || DeleteTag == "2") { this.spdSetList.ActiveSheet.Cells[rowCount, columnCount+1].Text = "3"; this.spdSetList.ActiveSheet.Cells[rowCount,columnCount+1].BackColor = clrWater; this.spdSetList.ActiveSheet.Rows[rowCount].Visible = false; } else if (DeleteTag == "1") { this.spdSetList.ActiveSheet.Rows[rowCount].Remove(); } } } } |
本文章摘自csdn,轉載請注明出處http://blog.csdn.net/jiezigege/article/details/4486642