using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace Example_1... ... { /**//**//**//// <summary> /// Summary description for Form1. /// </summary> public class frmProject : System.Windows.Forms.Form ......{ private System.Windows.Forms.Button btnCreate; private System.Windows.Forms.Button btnClose; private System.Windows.Forms.ListBox lstProjectValues; private System.Windows.Forms.Button btnView; /**//**//**//// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; //Data related variables private DataSet objDataSet; private DataTable objProjectTable; private DataRow objDataRow; private System.Windows.Forms.HelpProvider HelpForForm; private System.Windows.Forms.Button btnRemove; public frmProject() ......{ // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /**//**//**//// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) ......{ if( disposing ) ......{ if (components != null) ......{ components.Dispose(); } } base.Dispose( disposing ); } Windows Form Designer generated codeWindows Form Designer generated code#region Windows Form Designer generated code /**//**//**//// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() ......{ this.btnCreate = new System.Windows.Forms.Button(); this.btnClose = new System.Windows.Forms.Button(); this.lstProjectValues = new System.Windows.Forms.ListBox(); this.btnView = new System.Windows.Forms.Button(); this.btnRemove = new System.Windows.Forms.Button(); this.HelpForForm = new System.Windows.Forms.HelpProvider(); this.SuspendLayout(); // // btnCreate // this.btnCreate.Location = new System.Drawing.Point(19, 215); this.btnCreate.Name = "btnCreate"; this.btnCreate.Size = new System.Drawing.Size(90, 25); this.btnCreate.TabIndex = 0; this.btnCreate.Text = "创建(&C)"; this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click); // // btnClose // this.btnClose.Location = new System.Drawing.Point(307, 215); this.btnClose.Name = "btnClose"; this.btnClose.Size = new System.Drawing.Size(90, 25); this.btnClose.TabIndex = 1; this.btnClose.Text = "关闭(&C)"; this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // lstProjectValues // this.lstProjectValues.Enabled = false; this.lstProjectValues.ItemHeight = 12; this.lstProjectValues.Location = new System.Drawing.Point(19, 17); this.lstProjectValues.MultiColumn = true; this.lstProjectValues.Name = "lstProjectValues"; this.lstProjectValues.Size = new System.Drawing.Size(375, 184); this.lstProjectValues.TabIndex = 2; // // btnView // this.btnView.Enabled = false; this.btnView.Location = new System.Drawing.Point(115, 215); this.btnView.Name = "btnView"; this.btnView.Size = new System.Drawing.Size(90, 25); this.btnView.TabIndex = 0; this.btnView.Text = "查看(&V)"; this.btnView.Click += new System.EventHandler(this.btnView_Click); // // btnRemove // this.btnRemove.Enabled = false; this.btnRemove.Location = new System.Drawing.Point(211, 215); this.btnRemove.Name = "btnRemove"; this.btnRemove.Size = new System.Drawing.Size(90, 25); this.btnRemove.TabIndex = 3; this.btnRemove.Text = "移除(&R)"; this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click); // // HelpForForm // this.HelpForForm.HelpNamespace = "E:WorkACCP4.0ProjectWinFormsWinformsLG9Ver1-SourcePart IDatabase Help.chm"; // // frmProject // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(422, 246); this.Controls.Add(this.btnRemove); this.Controls.Add(this.lstProjectValues); this.Controls.Add(this.btnClose); this.Controls.Add(this.btnCreate); this.Controls.Add(this.btnView); this.HelpForForm.SetHelpKeyword(this, ""); this.Name = "frmProject"; this.HelpForForm.SetShowHelp(this, true); this.Text = "添加行"; this.ResumeLayout(false); } #endregion /**//**//**//// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() ......{ Application.Run(new frmProject()); } private void btnCreate_Click(object sender, System.EventArgs e) ......{ //create dataset objDataSet = new DataSet(); //create a datatable objProjectTable = new DataTable("Project"); //adding the columns objProjectTable.Columns.Add(new DataColumn("ProjectID",typeof(int))); //adding the primary key objProjectTable.PrimaryKey = new DataColumn[1]......{objProjectTable.Columns["ProjectID"]}; objProjectTable.Columns.Add(new DataColumn("ProjectName",typeof(string))); objProjectTable.Columns.Add(new DataColumn("ProjectDescription",typeof(string))); objProjectTable.Columns.Add(new DataColumn("ClientID",typeof(string))); objProjectTable.Columns.Add(new DataColumn("EmployeeID",typeof(string))); //adding the table to dataset objDataSet.Tables.Add(objProjectTable); //creating a new row objDataRow = objProjectTable.NewRow(); objDataRow["ProjectID"]=101; objDataRow["ProjectName"]="Visual C# Inventory Project"; objDataRow["ProjectDescription"]="For ABC Compnay"; objDataRow["ClientID"]="C101"; objDataRow["EmployeeID"]="E01"; //adding the row to the table objProjectTable.Rows.Add(objDataRow); //creating a new row objDataRow = objProjectTable.NewRow(); objDataRow["ProjectID"]=102; objDataRow["ProjectName"]="Funding Project"; objDataRow["ProjectDescription"]="Developed in EJB, STRUTS"; objDataRow["ClientID"]="C102"; objDataRow["EmployeeID"]="E03"; //adding the row to the table objProjectTable.Rows.Add(objDataRow); MessageBox.Show("Project Table created with "+objProjectTable.Rows.Count.ToString() + " rows","Total Rows"); //enabling and disabling the controls this.btnView.Enabled = true; this.lstProjectValues.Enabled = true; this.btnCreate.Enabled = false; } private void btnClose_Click(object sender, System.EventArgs e) ......{ this.Close(); } private void btnView_Click(object sender, System.EventArgs e) ......{ //displaying the rows in the List Box foreach(DataRow row in objDataSet.Tables["Project"].Rows) ......{ foreach(DataColumn column in objDataSet.Tables["Project"].Columns) ......{ this.lstProjectValues.Items.Add(row[column].ToString()); } this.lstProjectValues.Items.Add(""); } this.btnView.Enabled=false; this.btnRemove.Enabled = true; } private void btnRemove_Click(object sender, System.EventArgs e) ......{ objProjectTable.Rows.Remove(objDataSet.Tables[0].Rows[0]); this.lstProjectValues.Items.Clear(); MessageBox.Show("Project Table has "+objProjectTable.Rows.Count.ToString() + " rows", " Total Rows"); btnView_Click(sender,e); } }}