关于在DataGrid里添加ComboBox(一)

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

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;

时间: 2024-07-30 11:34:40

关于在DataGrid里添加ComboBox(一)的相关文章

关于在DataGrid里添加ComboBox(三)

sjDataGridComboBox.csusing System.Collections;using System.ComponentModel;using System.Drawing;using System.Windows.Forms;using System.Data;namespace ComboBoxInDataGrid{    public class DataGridComboBox:ComboBox    {        //        // Basic ComboBo

关于在DataGrid里添加ComboBox(二)

忘记说了一和二共同组成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.b

嵌入式开发-在Window CE 操作系统中,如何在DataGrid 中添加ComboBox 控件

问题描述 在Window CE 操作系统中,如何在DataGrid 中添加ComboBox 控件 最近在开发手持机,因为公司要求,将ERP 系统置入到手持机中,发现在Window CE 系统中,很多 控件都无法使用.譬如说,要在DataGrid 中添加列,要求在DataGrid 中操作数据,下拉的 ComBobox 就没有,也没有DataGridComBoBox 列,如何处理,请各位兄弟指教. QQ:870088133 解决方案 CE版的控件是精简的,很多功能都没有.需要自己实现. 你说的添加C

关于在datagrid里添加超连接

问题描述 在Form里的datagrid,怎么动态地在datagrid里添加超连接啊,比如说我把一个字段变成超连接. 解决方案 解决方案二:用DataGridTableStyle个看能不能搞好,没试过.我只做过绑textbox的DataGridColoredTextBoxColumn.解决方案三:http://www.devage.com/研究一下这个控件的源码一定有收获.解决方案四:不明LZ在说什么

如何在DataGrid中添加ComboBox的方法

有两中方法:1.修改 ColumnStyles 属性的值.2.在DataGrid里, 直接贴 Combo 控件. 如:Public MyCombo As New ComboBox()    Dim con As NewSqlConnection("server=myservername;uid=myid;pwd=mypassword;database=northwind")    Dim daEmp As New SqlDataAdapter("Select * From E

一个关于DataGrid里使用Combobox 的奇怪问题,希望大家进来看看 帮忙解答一下

问题描述 <DataGridx:Name="grd3"Grid.Column="1"Style="{BindingSource={StaticResourcedgcommstyle}}"CanUserDeleteRows="False"SelectionMode="Single"><i:Interaction.Triggers><i:EventTriggerEventName=

在DataGrid里添加DropDownLit控件

datagrid|控件 ? Using DropDownList control in DataGridBy Eric Zheng When I was developing a web application couple days ago, I found some interesting things about the datagrid, I want to share them with other vs.net programmers, so I wrote this article

向DataGrid控件中添加ComboBox控件

combobox控件|datagrid|datagrid控件   在前面看到了很多关于怎样向DataGrid中添加ComboBox控件的方法.使用的方法全部都是在VB6.0中的方法. 我还是要说说在CSND中发贴的朋友. 现在所谓的.NET编程人员,不知道是怎么了呢!只是停留在使用.NET的编程环境中.并没有真正的了解面向对象的.NET编程思想. 我现在就利用继承DataGridColumnStyle完成向DataGrid中添加ComboBox. 希望这样有助于大家了解真正的面向对象编程的思想.

为什么不能向combobox里添加项

问题描述 为什么不能向combobox里添加项 cbPhoneKind.Items.Clear(); string[] cbPhoneKindName = { "三星", "联想", "诺基亚", "VIVO", "酷比", "摩托罗拉", "酷派", "金立", "HTC", "魅族", "小米&