动态改变一列的数据类型,比如说单击某一列时,使其变为下拉框列。
如图:
单击前(
假设单击第二列使其变为下拉框)
单击后:
实现如上效果:
代码如下:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; using TRE.Shared.DBProcessException;
namespace AlterColumnType { /// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form {
TRE.Shared.DBProcess DBProc = new TRE.Shared.DBProcess("MasterMaintenance"); private FarPoint.Win.Spread.FpSpread fpSpread1; private FarPoint.Win.Spread.SheetView fpSpread1_Sheet1; /// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1() { //
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows フォーム デザイナで生成されたコード /// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent() { this.fpSpread1 = new FarPoint.Win.Spread.FpSpread(); this.fpSpread1_Sheet1 = new FarPoint.Win.Spread.SheetView(); ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.fpSpread1_Sheet1)).BeginInit(); this.SuspendLayout(); //
// fpSpread1
//
this.fpSpread1.About = "1.1.0.0"; this.fpSpread1.AccessibleDescription = ""; this.fpSpread1.Location = new System.Drawing.Point(8, 16); this.fpSpread1.Name = "fpSpread1"; this.fpSpread1.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] { this.fpSpread1_Sheet1}); this.fpSpread1.Size = new System.Drawing.Size(368, 296); this.fpSpread1.TabIndex = 0; this.fpSpread1.CellClick += new FarPoint.Win.Spread.CellClickEventHandler(this.fpSpread1_CellClick); //
// fpSpread1_Sheet1
//
this.fpSpread1_Sheet1.Reset(); this.fpSpread1_Sheet1.SheetName = "Sheet1"; //
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12); this.ClientSize = new System.Drawing.Size(384, 350); this.Controls.Add(this.fpSpread1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.fpSpread1_Sheet1)).EndInit(); this.ResumeLayout(false);
} #endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread] static void Main() { Application.Run(new Form1()); }
private void fpSpread1_CellClick(object sender, FarPoint.Win.Spread.CellClickEventArgs e) { if(e.ColumnHeader && e.Column == 1) { #region 连接数据库,请替换为自己的连接代码 string select = "select top 10 AreaName from Areas"; DBProc.Open(); DataSet ds = DBProc.OpenDataSet(select); DBProc.Close();
FarPoint.Win.Spread.CellType.ComboBoxCellType cb = new FarPoint.Win.Spread.CellType.ComboBoxCellType(); cb.ListWidth = 150; cb.Editable = true; cb.MaxDrop = 10; cb.MaxLength = 8; string[] employeeList = DataSetToArray(ds, 8); cb.Items = employeeList; this.fpSpread1.ActiveSheet.Columns[1].CellType = cb; } } 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(); i++; } return returnArray; } } }
|