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;
}
}
- datagridview combobox
- datagridview定制ComboBox列
- DataGridView中加ComboBox
- DataGridView添加ComboBox
- DataGridView添加ComboBox
- Datagridview中的ComboBox
- DataGridview add Combobox
- Datagridview add Combobox 2
- DataGridView里动态添加comboBox
- C# DataGridView combobox可编辑
- DataGridView 设置ComboBox 数据源设置
- 在dataGridView中显示combobox
- Combobox弹出下拉的dataGridView
- DataGridView 中动态加入ComboBox
- C#DataGridView中加ComboBox
- DataGridView 中Combobox 重载事件
- datagridview中ComboBox的数据源
- DataGridView中的Combobox的应用
- dropbox
- 透明显示原理及其实现 [转]
- 生活哲学:学C++快四年了,又回头细看C++Primer,觉得有些东西陪伴了你几年了,你也未必了解它。
- Nginx + tomcat + SSL 安装配置手册
- 04/25
- datagridview combobox
- session共享
- word2007失效解决办法
- export 批量导数据(分区)脚本(手动)
- 堆与栈
- lpc2103 软件中断原理
- Object Initializer 使用名字初始化类的成员或属性
- 打印js中一个对象的所有属性的值
- jfreechart 中文api