引自:梦在脚下,让心灵去飞翔
http://www.cnblogs.com/CodeAnyWhere/archive/2005/12/13/296461.html
项目需要,稍微修改了ComboBoxDataGridView


代码如下:
客户端调用代码:
http://www.cnblogs.com/CodeAnyWhere/archive/2005/12/13/296461.html
项目需要,稍微修改了ComboBoxDataGridView
代码如下:
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Data;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Drawing.Design;
9
10
namespace WindowsApplication21
11
{
12
public class ComboBoxDataGridView : ComboBox
13
{
14
成员变量
29
30
构造函数
43
44
属性
82
83
实现方法
204
}
205
}
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Drawing;5
using System.Data;6
using System.Text;7
using System.Windows.Forms;8
using System.Drawing.Design;9

10
namespace WindowsApplication2111
{12
public class ComboBoxDataGridView : ComboBox13
{14
成员变量29

30
构造函数43

44
属性82

83
实现方法204
}205
}客户端调用代码:
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Data.SqlClient;
6
using System.Drawing;
7
using System.Text;
8
using System.Windows.Forms;
9
10
namespace 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
}
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Data.SqlClient;6
using System.Drawing;7
using System.Text;8
using System.Windows.Forms;9

10
namespace WindowsApplication2111
{12
public partial class Form2 : Form13
{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
}



浙公网安备 33010602011771号