1. 在datagrid中加入combobox列;
2. 把在datagrid中的修改保存到对应的网格;
3. 设置datagrid中网格的焦点。
下面是整个源代码,一些功能可以看注释。
1
namespace datagridtest
2

{
3
public class form1 : system.windows.forms.form
4
{
5
private system.windows.forms.datagrid dgdfunctionarea;
6
private datatable dtblfunctionalarea;
7
private system.windows.forms.button buttonfocus;
8
private system.componentmodel.container components = null;
9
10
public form1()
11
{
12
initializecomponent();
13
populategrid();
14
}
15
16
protected override void dispose( bool disposing )
17
{
18
if( disposing )
19
{
20
if (components != null)
21
{
22
components.dispose();
23
}
24
}
25
base.dispose( disposing );
26
}
27
28
windows 窗体设计器生成的代码#region windows 窗体设计器生成的代码
29
30
private void initializecomponent()
31
{
32
this.dgdfunctionarea = new system.windows.forms.datagrid();
33
this.buttonfocus = new system.windows.forms.button();
34
((system.componentmodel.isupportinitialize)(this.dgdfunctionarea)).begininit();
35
this.suspendlayout();
36
/
37
/ dgdfunctionarea
38
/
39
this.dgdfunctionarea.datamember = "";
40
this.dgdfunctionarea.headerforecolor = system.drawing.systemcolors.controltext;
41
42
this.dgdfunctionarea.location = new system.drawing.point(4, 8);
43
this.dgdfunctionarea.name = "dgdfunctionarea";
44
this.dgdfunctionarea.size = new system.drawing.size(316, 168);
45
this.dgdfunctionarea.tabindex = 0;
46
/
47
/ buttonfocus
48
/
49
this.buttonfocus.location = new system.drawing.point(232, 188);
50
this.buttonfocus.name = "buttonfocus";
51
this.buttonfocus.size = new system.drawing.size(84, 23);
52
this.buttonfocus.tabindex = 1;
53
this.buttonfocus.text = "获取焦点";
54
this.buttonfocus.click += new system.eventhandler(this.buttonfocus_click);
55
/
56
/ form1
57
/
58
this.autoscalebasesize = new system.drawing.size(6, 14);
59
this.clientsize = new system.drawing.size(332, 217);
60
this.controls.add(this.buttonfocus);
61
this.controls.add(this.dgdfunctionarea);
62
this.name = "form1";
63
this.text = "form1";
64
((system.componentmodel.isupportinitialize)(this.dgdfunctionarea)).endinit();
65
this.resumelayout(false);
66
67
}
68
#endregion
69
// <summary>
70
// 应用程序的主入口点。
71
// </summary>
72
[stathread]
73
static void main()
74
{
75
application.run(new form1());
76
}
77
/初始化datagrid
78
private void populategrid()
79
{
80
/创建一个datatable对象,包括四列,前三列为string,最后一列为boolean。
81
dtblfunctionalarea = new datatable ("functionarea");
82
string[] arrstrfunctionalarea = new string [3]
{"functional area","min","max"};
83
datacolumn dtcol = null;
84
/创建string列
85
for(int i=0; i< 3;i++)
86
{
87
dtcol = new datacolumn(arrstrfunctionalarea[i]);
88
dtcol.datatype = type.gettype("system.string");
89
dtcol.defaultvalue = "";
90
dtblfunctionalarea.columns.add(dtcol);
91
}
92
93
/创建boolean列,用checkedbox来显示。
94
datacolumn dtccheck = new datacolumn("ismandatory");
95
dtccheck.datatype = system.type.gettype("system.boolean");
96
dtccheck.defaultvalue = false;
97
dtblfunctionalarea.columns.add(dtccheck);
98
99
/把表绑定到datagrid
100
dgdfunctionarea.datasource = dtblfunctionalarea;
101
102
/为datagrid加载datagridtablestyle样式
103
if(!dgdfunctionarea.tablestyles.contains("functionarea"))
104
{
105
datagridtablestyle dgdtblstyle = new datagridtablestyle();
106
dgdtblstyle.mappingname = dtblfunctionalarea.tablename;
107
dgdfunctionarea.tablestyles.add(dgdtblstyle);
108
dgdtblstyle.rowheadersvisible = false;
109
dgdtblstyle.headerbackcolor = color.lightsteelblue;
110
dgdtblstyle.allowsorting = false;
111
dgdtblstyle.headerbackcolor = color.fromargb(8,36,107);
112
dgdtblstyle.rowheadersvisible = false;
113
dgdtblstyle.headerforecolor = color.white;
114
dgdtblstyle.headerfont = new system.drawing.font("microsoft sans serif", 9f,
115
system.drawing.fontstyle.bold,
116
system.drawing.graphicsunit.point, ((system.byte)(0)));
117
dgdtblstyle.gridlinecolor = color.darkgray;
118
dgdtblstyle.preferredrowheight = 22;
119
dgdfunctionarea.backgroundcolor = color.white;
120
121
/设置列的宽度
122
gridcolumnstylescollection colstyle = dgdfunctionarea.tablestyles[0].gridcolumnstyles;
123
colstyle[0].width = 100;
124
colstyle[1].width = 50;
125
colstyle[2].width = 50;
126
colstyle[3].width = 80;
127
}
128
129
datagridtextboxcolumn dgtb = (datagridtextboxcolumn)dgdfunctionarea.tablestyles[0].gridcolumnstyles[0];
130
131
combobox cmbfunctionarea = new combobox();
132
cmbfunctionarea.items.addrange(new object[]
{"选项一","选项二","选项三"});
133
cmbfunctionarea.cursor = cursors.arrow;
134
cmbfunctionarea.dropdownstyle= comboboxstyle.dropdownlist;
135
cmbfunctionarea.dock = dockstyle.fill;
136
137
/在选定项发生更改并且提交了该更改后发生
138
139
cmbfunctionarea.selectionchangecommitted += new eventhandler(cmbfunctionarea_selectionchangecommitted);
140
141
/把combobox添加到datagridtablestyle的第一列
142
143
dgtb.textbox.controls.add(cmbfunctionarea);
144
145
}
146
147
/设置焦点模拟
148
149
private void getfocus(int row,int col)
150
{
151
/先把焦点移动到datagrid
152
this.dgdfunctionarea.focus();
153
/把焦点移动到datagridcell
154
datagridcell dgc = new datagridcell(row,col);
155
this.dgdfunctionarea.currentcell = dgc;
156
datagridtextboxcolumn dgtb = (datagridtextboxcolumn)dgdfunctionarea.tablestyles[0].gridcolumnstyles[col];
157
158
/设置焦点
159
160
dgtb.textbox.focus();
161
}
162
163
/把combobox上修改的数据提交到当前的网格
164
165
private void cmbfunctionarea_selectionchangecommitted(object sender, eventargs e)
166
{
167
this.dgdfunctionarea[this.dgdfunctionarea.currentcell] = ((combobox)sender).selecteditem.tostring();
168
169
}
170
171
/设置新的焦点
172
173
private void buttonfocus_click(object sender, system.eventargs e)
174
{
175
/焦点模拟,这里设置第三行第一列
176
getfocus(2,0);
177
}
178
}
179
180
}
181

2



3

4



5

6

7

8

9

10

11



12

13

14

15

16

17



18

19



20

21



22

23

24

25

26

27

28


29

30

31



32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74



75

76

77

78

79



80

81

82



83

84

85

86



87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104



105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132



133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150



151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166



167

168

169

170

171

172

173

174



175

176

177

178

179

180

181

总结,这里是通过datagridtextboxcolumn.textbox.controls.add方法实现在列中添加combobox控件;对于数据的保存是使用combobox.selectionchangecommitted事件来完成;设置焦点是通过datagridtextboxcolumn.textbox.focus方法来实现。另外通过这个方法也可以添加datetimepicker等类似的控件。