在.NET中的DataGrid控件中只有两种列样式:①DataGridTextBoxColumn;②DataGridBoolColumn
如要添加combox样式列,可以下面方法进行
1、申明并创建一个对象实例,并进行初始化
Private sub cbobox as new combox'模块级的
AddHandler cbobox.Textchanged,addressof Ctrls_Textchanged
cbobox.name="cbobox"
cbobox.visible=False
cbobox.dropdownstyle=comboxstyle.dropdown
cbobox.items.clear
cbobox.items.Add("a")
datagrid.Controls.add(cbobox)'将对象添加到DATAGRID控件集中去
2、以下为对象的Ctrls_Textchanged事件
Private sub Ctrls_Textchanged(sender as object,e as system.EventArgs)
‘判断DATAGRID的当前单元格是否是您想要在此添加下拉列表控件的列(在DATAGRID列是从0开始)
if DataGrid.CurrentCell.ColunmNumber =4 then'在此我假设为第5列
if DataGrid.Items(DataGrid.CurrentCell) & "" = "" then
SendKeys.Send(" ")'如果单元格为空,则向程序发送一个空格
end if
DataGrid.Items(DataGrid.CurrentCell)=cbobox.Text'设置当前选定的单元格为CBOBOX选定项的内容
End If
end sub
3、为DataGrid添加Click、CurrentCellChanged、Paint、Scroll事件
其中Click和Scroll事件是隐藏对象的,代码如下:
cbobox.Visible=False
cbobox.Width=0
在Paint事件中设置CboBox的Width,让其可以看见,让您操作,代码如下:
If DataGrid.CurrentCell.ColumnNumber=4 then
cbobox.width=DataGrid.GetCurrentCellBounds.Width
End if
在CurrentCellChanged事件中处理对象是否进行显示,代码如下:
If DataGrid.CurrentCell.ColumnNumber=4 then
cbobox.Visible=False
cbobox.Width=0
cbobox.left=DataGrid.GetCurrentCellBounds.Left
cbobox.Top=DataGrid.GetCurrentCellBounds.Top
cbobox.text=DataGrid.Items(DataGrid.CurrentCell) & ""
cbobox.Visible=True
else
cbobox.visible=False
cbobox.width=0
end if
加進去有問題
不知道怎麼回事
更新第二次時
combobox的items中出現其它列
Dim i As Integer = 0
sqlstr = "SELECT distinct bmbh02 部門ID, bmbh02mc FROM dbo.hp_bmbhA_02"
dtdep.Clear()
dtdep = sqlexecute.GetDataTable(sqlstr)
If dtdep.Rows.Count - 1 >= 0 Then
Me.ComboBoxDepId.Items.Clear()
For i = 0 To dtdep.Rows.Count - 1
Me.ComboBoxDepId.Items.Add(dtdep.Rows(i).Item(0).ToString.Trim)
Next
End If