关于在DataGrid里添加ComboBox(一)

80酷酷网    80kuku.com

  

这段代码我没整理过,希望哪位整理一下重新贴上来

using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Globalization;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data.SqlClient;

namespace ComboBoxInDataGrid
{
    public class Form2:System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private SqlDataAdapter sqlDA;
        private SqlCommand sqlSelectStudent;
        private SqlCommand sqlInsertStudent;
        private SqlCommand sqlUpdateStudent;
        private SqlCommand sqlDeleteStudent;
        private System.ComponentModel.Container components = null;
        private string StrSQL;
        private SqlConnection sqlConn;
        private DataSet _StudentDS;
        private DataTable _CourseDT;
        private DataGridTableStyle GridTableStyle;
        private System.Windows.Forms.Button btnUpdate;
        private System.Windows.Forms.Button btnMoveFirst;
        private System.Windows.Forms.Button btnMovePrevious;
        private System.Windows.Forms.Button btnMoveNext;
        private System.Windows.Forms.Button btnMoveLast;
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.Windows.Forms.Label label1;
        private CurrencyManager objStudentCM;
        public Form2()
        {
            //
            // 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 );
        }
    #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.sqlUpdateStudent = new System.Data.SqlClient.SqlCommand();
            this.sqlConn = new System.Data.SqlClient.SqlConnection();
            this.sqlDA = new System.Data.SqlClient.SqlDataAdapter();
            this.sqlDeleteStudent = new System.Data.SqlClient.SqlCommand();
            this.sqlInsertStudent = new System.Data.SqlClient.SqlCommand();
            this.sqlSelectStudent = new System.Data.SqlClient.SqlCommand();
            this.btnMoveLast = new System.Windows.Forms.Button();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.btnUpdate = new System.Windows.Forms.Button();
            this.btnMoveNext = new System.Windows.Forms.Button();
            this.btnMoveFirst = new System.Windows.Forms.Button();
            this.btnMovePrevious = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.SuspendLayout();
            //
            // sqlUpdateStudent
            //
            this.sqlUpdateStudent.CommandText = "UPDATE Student SET SNo = SNo, SName = SName, Course = Course WHERE (SNo = Original_SNo) AND (Course = Original_Course OR Original_Course1 IS NULL AND Course IS NULL) AND (SName = Original_SName OR Original_SName1 IS NULL AND SName IS NULL); SELECT SNo, SName, Course FROM Student WHERE (SNo = Select_SNo)";
            this.sqlUpdateStudent.Connection = this.sqlConn;
            this.sqlUpdateStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("SNo", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SNo", System.Data.DataRowVersion.Current, null));
            this.sqlUpdateStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("SName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "SName", System.Data.DataRowVersion.Current, null));
            this.sqlUpdateStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Course", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Course", System.Data.DataRowVersion.Current, null));
            this.sqlUpdateStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Original_SNo", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SNo", System.Data.DataRowVersion.Original, null));
            this.sqlUpdateStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Original_Course", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Course", System.Data.DataRowVersion.Original, null));
            this.sqlUpdateStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Original_Course1", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Course", System.Data.DataRowVersion.Original, null));
            this.sqlUpdateStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Original_SName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "SName", System.Data.DataRowVersion.Original, null));
            this.sqlUpdateStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Original_SName1", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "SName", System.Data.DataRowVersion.Original, null));
            this.sqlUpdateStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Select_SNo", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SNo", System.Data.DataRowVersion.Current, null));
            //
            // sqlConn
            //
            this.sqlConn.ConnectionString = "data source=localhost;initial catalog=myData;persist security info=False;user id=" +
                "sa;workstation id=NWAD-SJALLI;packet size=4096";
            //
            // sqlDA
            //
            this.sqlDA.DeleteCommand = this.sqlDeleteStudent;
            this.sqlDA.InsertCommand = this.sqlInsertStudent;
            this.sqlDA.SelectCommand = this.sqlSelectStudent;
            this.sqlDA.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                                                                                            new System.Data.Common.DataTableMapping("Table", "Student", new System.Data.Common.DataColumnMapping[] {
                                                                                                                                                                                                       new System.Data.Common.DataColumnMapping("SNo", "SNo"),
                                                                                                                                                                                                       new System.Data.Common.DataColumnMapping("SName", "SName"),
                                                                                                                                                                                                       new System.Data.Common.DataColumnMapping("Course", "Course")})});
            this.sqlDA.UpdateCommand = this.sqlUpdateStudent;
            //
            // sqlDeleteStudent
            //
            this.sqlDeleteStudent.CommandText = "DELETE FROM Student WHERE (SNo = SNo) AND (Course = Course OR Course1 IS NULL " +
                "AND Course IS NULL) AND (SName = SName OR SName1 IS NULL AND SName IS NULL)";
            this.sqlDeleteStudent.Connection = this.sqlConn;
            this.sqlDeleteStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("SNo", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SNo", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Course", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Course", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Course1", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Course", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("SName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "SName", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("SName1", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "SName", System.Data.DataRowVersion.Original, null));
            //
            // sqlInsertStudent
            //
            this.sqlInsertStudent.CommandText = "INSERT INTO Student(SNo, SName, Course) VALUES (SNo, SName, Course); SELECT SN" +
                "o, SName, Course FROM Student WHERE (SNo = Select_SNo)";
            this.sqlInsertStudent.Connection = this.sqlConn;
            this.sqlInsertStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("SNo", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SNo", System.Data.DataRowVersion.Current, null));
            this.sqlInsertStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("SName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "SName", System.Data.DataRowVersion.Current, null));
            this.sqlInsertStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Course", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Course", System.Data.DataRowVersion.Current, null));
            this.sqlInsertStudent.Parameters.Add(new System.Data.SqlClient.SqlParameter("Select_SNo", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SNo", System.Data.DataRowVersion.Current, null));
            //
            // sqlSelectStudent
            //
            this.sqlSelectStudent.CommandText = "SELECT SNo, SName, Course FROM Student";
            this.sqlSelectStudent.Connection = this.sqlConn;
            //
            // btnMoveLast
            //
            this.btnMoveLast.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
            this.btnMoveLast.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btnMoveLast.Location = new System.Drawing.Point(128, 264);
            this.btnMoveLast.Name = "btnMoveLast";
            this.btnMoveLast.Size = new System.Drawing.Size(32, 24);
            this.btnMoveLast.TabIndex = 5;
            this.btnMoveLast.Text = "|>";
            this.btnMoveLast.Click += new System.EventHandler(this.btnMoveLast_Click);
            //
            // dataGrid1
            //
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right);
            this.dataGrid1.BackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.BackgroundColor = System.Drawing.Color.Beige;
            this.dataGrid1.CaptionBackColor = System.Drawing.Color.Blue;
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.Yellow;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.ForeColor = System.Drawing.Color.Navy;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Chocolate;
            this.dataGrid1.HeaderBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.Brown;
            this.dataGrid1.Location = new System.Drawing.Point(16, 8);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.PreferredColumnWidth = -1;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.Brown;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.Aqua;
            this.dataGrid1.Size = new System.Drawing.Size(512, 224);
            this.dataGrid1.TabIndex = 0;

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: