关于在DataGrid里添加ComboBox(二)

发表于:2007-06-30来源:作者:点击数: 标签:
忘记说了一和二共同组成form2.cs文件 // // btnUpdate // this.btnUpdate.Anchor = (System. Windows .Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right); this.btnUpdate.Location = new System.Drawing.Point(456, 264); this.btnUpda
忘记说了一和二共同组成form2.cs文件

//
            // btnUpdate
            //
            this.btnUpdate.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.btnUpdate.Location = new System.Drawing.Point(456, 264);
            this.btnUpdate.Name = "btnUpdate";
            this.btnUpdate.Size = new System.Drawing.Size(80, 24);
            this.btnUpdate.TabIndex = 1;
            this.btnUpdate.Text = "Update";
            this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
            //
            // btnMoveNext
            //
            this.btnMoveNext.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
            this.btnMoveNext.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btnMoveNext.Location = new System.Drawing.Point(96, 264);
            this.btnMoveNext.Name = "btnMoveNext";
            this.btnMoveNext.Size = new System.Drawing.Size(32, 24);
            this.btnMoveNext.TabIndex = 4;
            this.btnMoveNext.Text = ">";
            this.btnMoveNext.Click += new System.EventHandler(this.btnMoveNext_Click);
            //
            // btnMoveFirst
            //
            this.btnMoveFirst.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
            this.btnMoveFirst.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btnMoveFirst.Location = new System.Drawing.Point(32, 264);
            this.btnMoveFirst.Name = "btnMoveFirst";
            this.btnMoveFirst.Size = new System.Drawing.Size(32, 24);
            this.btnMoveFirst.TabIndex = 2;
            this.btnMoveFirst.Text = "<|";
            this.btnMoveFirst.Click += new System.EventHandler(this.btnMoveFirst_Click);
            //
            // btnMovePrevious
            //
            this.btnMovePrevious.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
            this.btnMovePrevious.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btnMovePrevious.Location = new System.Drawing.Point(64, 264);
            this.btnMovePrevious.Name = "btnMovePrevious";
            this.btnMovePrevious.Size = new System.Drawing.Size(32, 24);
            this.btnMovePrevious.TabIndex = 3;
            this.btnMovePrevious.Text = "<";
            this.btnMovePrevious.Click += new System.EventHandler(this.btnMovePrevious_Click);
            //
            // label1
            //
            this.label1.Location = new System.Drawing.Point(176, 264);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(104, 24);
            this.label1.TabIndex = 6;
            //
            // Form2
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(552, 301);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.label1,
                                                                          this.btnMoveLast,
                                                                          this.btnMoveNext,
                                                                          this.btnMovePrevious,
                                                                          this.btnMoveFirst,
                                                                          this.btnUpdate,
                                                                          this.dataGrid1});
            this.Name = "Form2";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.FrmDropDownColumn_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion
        private void FrmDropDownColumn_Load(object sender,System.EventArgs e)
        {
            int IntAvgCharWidth;
            //
            // Create instance of the dataset
            //
            _StudentDS = new DataSet();
            //
            // Create instance of the datatable
            //
            _CourseDT = new DataTable();
            //
            // Create instance of DataGridTableStyle object
            //
            GridTableStyle = new DataGridTableStyle();
            //        
            //         Calculate the width of the average character
            //         This gets used when we build the table styles
            //        
            IntAvgCharWidth=(int)(System.Drawing.Graphics.FromHwnd(this.Handle).MeasureString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.Font).Width/26);
            try
            {
                this.sqlConn.Open();
                sqlDA.Fill(_StudentDS,"Student");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //
            // Connect to the Course table
            //
            StrSQL = "SELECT * FROM Course";
            try
            {
                SqlDataAdapter da1= new SqlDataAdapter(StrSQL,sqlConn);
                da1.Fill(_StudentDS, "Course");
                da1.Dispose();
                sqlConn.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //
            // Get handles to the currency manager for the Student table
            // We@#ll use this to build the grid displays
            //
            objStudentCM = (System.Windows.Forms.CurrencyManager)this.BindingContext[_StudentDS.Tables][0]];
                                               
            //
            // Setup the Student information display
            
            GridTableStyle.MappingName = "Student";
            _CourseDT = _StudentDS.Tables["Course"];
            // .Add(column:=new DataGridComboBoxColumnStyle(DataSource:=_CourseDT, _
            //                                             DisplayMember:="Course", _
            //                                             ValueMember:="Course"))


            GridTableStyle.GridColumnStyles.Add(new DataGridTextBoxColumn(objStudentCM .GetItemProperties()["SNo"]));
            GridTableStyle.GridColumnStyles[0].MappingName = "SNo";
            GridTableStyle.GridColumnStyles[0].HeaderText = "Student No";
            GridTableStyle.GridColumnStyles[0].Alignment = HorizontalAlignment.Left;
            //GridTableStyle.GridColumnStyles[0].Width = IntAvgCharWidth * 20;
            GridTableStyle.GridColumnStyles[0].NullText = string.Empty;

            GridTableStyle.GridColumnStyles.Add(new DataGridTextBoxColumn(objStudentCM .GetItemProperties()["SName"]));
            GridTableStyle.GridColumnStyles[1].MappingName = "SName";
            GridTableStyle.GridColumnStyles[1].HeaderText = "Student Name";
            GridTableStyle.GridColumnStyles[1].Alignment = HorizontalAlignment.Left;
            //GridTableStyle.GridColumnStyles[1].Width = IntAvgCharWidth * 20;
            GridTableStyle.GridColumnStyles[1].NullText = string.Empty;

            GridTableStyle.GridColumnStyles.Add(new DataGridComboBoxColumn(_CourseDT,0,0));
            GridTableStyle.GridColumnStyles[2].MappingName = "Course";
            GridTableStyle.GridColumnStyles[2].HeaderText = "Course";
            GridTableStyle.GridColumnStyles[2].Alignment = HorizontalAlignment.Left;
            GridTableStyle.GridColumnStyles[2].Width = IntAvgCharWidth * 20;
            GridTableStyle.GridColumnStyles[2].NullText = string.Empty;

            
            //
            // Wire up the grid control
            //

            dataGrid1.CaptionText = string.Empty;
            dataGrid1.DataSource = _StudentDS;
            dataGrid1.DataMember = "Student";
            dataGrid1.TableStyles.Add(GridTableStyle);
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        
        [STAThread]
        static void Main()
        {
            Application.Run(new Form2());
        }

        // Updates the _StudentDS with changes
        private void btnUpdate_Click(object sender, System.EventArgs e)
        {
            this.sqlDA.Update(_StudentDS,"Student");
        }
        // Move to the Last Record
        private void btnMoveLast_Click(object sender, System.EventArgs e)
        {
            this.BindingContext[_StudentDS,"Student"].Position = this.BindingContext[_StudentDS,"Student"].Count;
        }
        // Move to the Next Record
        private void btnMoveNext_Click(object sender, System.EventArgs e)
        {
            this.BindingContext[_StudentDS,"Student"].Position += 1;
        }
        // Move to the Previous Record
        private void btnMovePrevious_Click(object sender, System.EventArgs e)
        {
            this.BindingContext[_StudentDS,"Student"].Position -= 1;
        }
        // Move to the First Record
        private void btnMoveFirst_Click(object sender, System.EventArgs e)
        {
            this.BindingContext[_StudentDS,"Student"].Position = 0;
        }
    }
}

原文转自:http://www.ltesting.net