datagridview combobox

来源:互联网 发布:mac设置maven环境变量 编辑:程序博客网 时间:2024/05/16 17:21

Form1.cs:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      

        public List<Area> GetData(string sql)
        {
            List<Area> aes = new List<Area>();
            OleDbConnection conn = oleDbConnection1;

            OleDbCommand comm = new OleDbCommand();
            comm.CommandText = sql;

            comm.Connection = conn;
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            OleDbDataReader dr =  comm.ExecuteReader(CommandBehavior.CloseConnection);
            while (dr.Read())
            {
                Area a = new Area();
                a.AreaID = Convert.ToInt32( dr["AreaID"]);
                a.AreaName = dr["AreaName"].ToString();
                a.FatherID = Convert.ToInt32( dr["FatherID"]);
                aes.Add(a);
            }
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            dr.Dispose();

            return aes;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          

         

            this.Name1.DataSource = GetData("select * from area where fatherid = 0");
            this.Name1.DisplayMember = "AreaName";
            this.Name1.HeaderText = "Column1";
            this.Name1.Name = "Name1";
            this.Name1.ValueMember = "AreaID";
            this.Name1.Selected=true;
            //dataGridView1.DataSource = aes;
          
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                return;
            }
            label1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
           // label1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].GetType().ToString();

            DataGridViewComboBoxCell combox2 = dataGridView1.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell;
         


            combox2.DataSource = GetData("select * from area where fatherid = " + label1.Text);
            combox2.DisplayMember = "AreaName";
        
            combox2.ValueMember = "AreaID";
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

     
    }

    public class Area
    {

        private int areaID, fatherID;

        public int AreaID
        {
            get { return areaID; }
            set { areaID = value; }
        }

        public int FatherID
        {
            get { return fatherID; }
            set { fatherID = value; }
        }

        private string areaName;

        public string AreaName
        {
            get { return areaName; }
            set { areaName = value; }
        }
    }
}


Form1.Designer.cs:

 

namespace WindowsFormsApplication2
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.Name1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
            this.Name2 = new System.Windows.Forms.DataGridViewComboBoxColumn();
            this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            //
            // dataGridView1
            //
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Name1,
            this.Name2});
            this.dataGridView1.Location = new System.Drawing.Point(40, 50);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowTemplate.Height = 23;
            this.dataGridView1.Size = new System.Drawing.Size(563, 291);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit);
            this.dataGridView1.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView1_DataError);
            //
            // Name1
            //
            this.Name1.HeaderText = "Column1";
            this.Name1.Name = "Name1";
            //
            // Name2
            //
            this.Name2.HeaderText = "Column2";
            this.Name2.Name = "Name2";
            //
            // oleDbConnection1
            //
            this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=/"db1.mdb/"";
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(430, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "label1";
            //
            // comboBox1
            //
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(288, 6);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 20);
            this.comboBox1.TabIndex = 2;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(630, 353);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Data.OleDb.OleDbConnection oleDbConnection1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.DataGridViewComboBoxColumn Name1;
        private System.Windows.Forms.DataGridViewComboBoxColumn Name2;
        private System.Windows.Forms.ComboBox comboBox1;
    }
}

 

 

原创粉丝点击