引自:梦在脚下,让心灵去飞翔
http://www.cnblogs.com/CodeAnyWhere/archive/2005/12/13/296461.html
项目需要,稍微修改了ComboBoxDataGridView
代码如下:
客户端调用代码:
http://www.cnblogs.com/CodeAnyWhere/archive/2005/12/13/296461.html
项目需要,稍微修改了ComboBoxDataGridView
代码如下:
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8using System.Drawing.Design;
9
10namespace WindowsApplication21
11{
12 public class ComboBoxDataGridView : ComboBox
13 {
14 成员变量
29
30 构造函数
43
44 属性
82
83 实现方法
204 }
205}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8using System.Drawing.Design;
9
10namespace WindowsApplication21
11{
12 public class ComboBoxDataGridView : ComboBox
13 {
14 成员变量
29
30 构造函数
43
44 属性
82
83 实现方法
204 }
205}
客户端调用代码:
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Data.SqlClient;
6using System.Drawing;
7using System.Text;
8using System.Windows.Forms;
9
10namespace WindowsApplication21
11{
12 public partial class Form2 : Form
13 {
14 DataTable dt = new DataTable();
15 public Form2() //构造函数
16 {
17 InitializeComponent();
18 cmbdgv.DropDownWidth = 400;
19 cmbdgv.DropDownHeight = 200;
20 using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=;database=smls"))
21 {
22 SqlDataAdapter sqlda = new SqlDataAdapter("select std_sg_code,std_text,sg_text,update_user_id from qp_standard_steel_grade", sqlconn);
23 sqlda.Fill(dt);
24 }
25 cmbdgv.DataGridView.DataSource = dt;
26 cmbdgv.DefaultColumn = "std_text"; //默认列
27 cmbdgv.AfterSelector += new EventHandler(cmbdgvAREA_NAME_AfterSelector);
28 for (int i = 0; i < cmbdgv.DataGridView.Columns.Count; i++)
29 {
30 cmbdgv.DataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
31 }
32 }
33
34 public void cmbdgv_AfterSelector(object sender, EventArgs e)
35 {
36 this.textBox1.Text = cmbdgv.GetDataProperty("std_sg_code"); //获得当前行某单元格的值
37 this.textBox2.Text = cmbdgv.GetDataProperty("sg_text");
38 this.textBox3.Text = cmbdgv.GetDataProperty("update_user_id");
39
40 }
41 }
42}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Data.SqlClient;
6using System.Drawing;
7using System.Text;
8using System.Windows.Forms;
9
10namespace WindowsApplication21
11{
12 public partial class Form2 : Form
13 {
14 DataTable dt = new DataTable();
15 public Form2() //构造函数
16 {
17 InitializeComponent();
18 cmbdgv.DropDownWidth = 400;
19 cmbdgv.DropDownHeight = 200;
20 using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=;database=smls"))
21 {
22 SqlDataAdapter sqlda = new SqlDataAdapter("select std_sg_code,std_text,sg_text,update_user_id from qp_standard_steel_grade", sqlconn);
23 sqlda.Fill(dt);
24 }
25 cmbdgv.DataGridView.DataSource = dt;
26 cmbdgv.DefaultColumn = "std_text"; //默认列
27 cmbdgv.AfterSelector += new EventHandler(cmbdgvAREA_NAME_AfterSelector);
28 for (int i = 0; i < cmbdgv.DataGridView.Columns.Count; i++)
29 {
30 cmbdgv.DataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
31 }
32 }
33
34 public void cmbdgv_AfterSelector(object sender, EventArgs e)
35 {
36 this.textBox1.Text = cmbdgv.GetDataProperty("std_sg_code"); //获得当前行某单元格的值
37 this.textBox2.Text = cmbdgv.GetDataProperty("sg_text");
38 this.textBox3.Text = cmbdgv.GetDataProperty("update_user_id");
39
40 }
41 }
42}