DataGridViewComboBox一些例子
参考网络某些程序,修改,测试界面:
PS 改用自定义控件界面:
修改后的代码:
Public Class Form1 'Private cmb As ComboBox = New ComboBox 'cmb这里准备采用自定义的多列的comBox Private WithEvents cmb As NeMoCmbBox = New NeMoCmbBox() 'cmb这里准备采用自定义的多列的NeMoCmbBox Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click '参考网络某些程序,修改,测试 Dim column As New DataGridViewComboBoxColumn() '在ComboBox的List中表示指定的项目 column.Items.Add("星期日") column.Items.Add("星期一") column.Items.Add("星期二") column.Items.Add("星期三") column.Items.Add("星期四") column.Items.Add("星期五") column.Items.Add("星期六") column.DataPropertyName = "Week" column.DisplayStyleForCurrentCellOnly = True column.Name = "Week" 'ComboBox列代替"Week"列表示 DataGridView1.Columns.Add(column) '插入到某一列的前面 'DataGridView1.Columns.Insert(index,column) '上面的例子中使用了Items属性设定, '下面介绍的是使用DataSource属性设定在ComboBox中表示的项目。 '在ComboBox中表示的List作成 Dim weekTable As New DataTable("WeekTable") weekTable.Columns.Add("Display",GetType(String)) weekTable.Columns.Add("Value",GetType(Integer)) weekTable.Rows.Add("星期日",0) weekTable.Rows.Add("星期一",1) weekTable.Rows.Add("星期二",2) weekTable.Rows.Add("星期三",3) weekTable.Rows.Add("星期四",4) weekTable.Rows.Add("星期五",5) weekTable.Rows.Add("星期六",6) 'DataGridViewComboBoxColumn作成 Dim col As New DataGridViewComboBoxColumn() '在"Week"列中表示被Bind的数据 col.DataPropertyName = "WeekS" 'DataGridViewComboBoxColumn的DataSource设定 col.DataSource = weekTable '实际值为"Value"列,表示文本的为"Display"列 col.ValueMember = "Value" col.DisplayMember = "Display" col.Name = "DataSource" '设置一些显示样式。 col.DisplayStyleForCurrentCellOnly = True col.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing '向DataGridView1追加 DataGridView1.Columns.Add(col) End Sub Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load '插入两个行。 Dim c As New DataGridViewTextBoxColumn c.Name = "textBox" DataGridView1.Columns.Add(c) c = New DataGridViewTextBoxColumn c.Name = "cBox" DataGridView1.Columns.Add(c) '--初始化comBox的数据项 'cmb.Items.Add("aaaa") 'cmb.Items.Add("bbbb") 'cmb.Items.Add("cccc") 'cmb.Items.Add("dddd") 'cmb.Items.Add("5555") '--- cmb.Table = NewTB() cmb.DisplayMember = "名称" cmb.ValueMember = "类型" cmb.LvWidth = 250 '--- cmb.Visible = False '加入comBox的事件,并加入到DataGridView1 Me.DataGridView1.Controls.Add(cmb) '-- Me.DataGridView1.ClearSelection() End Sub Private Sub Button2_Click(ByVal sender As Object,ByVal e As EventArgs) Handles Button2.Click MessageBox.Show(Me.DataGridView1.RowCount) End Sub Private Sub cmb_SelectedIndexChanged(ByVal sender As Object,ByVal e As EventArgs) Handles cmb.SelectedIndexChanged '此事件处理无效,因为index 一直都是-1 'MsgBox(Me.cmb.SelectedIndex) 'Me.DataGridView1.CurrentCell.Value = cmb.Text ' Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(1).Value = cmb.SelectedIndex End Sub Private Sub cmb_AfterRowSec(ByVal sender As Object,ByVal e As SelectEventArgs) Handles cmb.AfterRowSelectEvent Me.DataGridView1.CurrentCell.Value = cmb.Text Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(1).Value = cmb.SelectedRow(2) Me.Text = String.Format("SelectedItemIndex:{0}",cmb.SelectedItemIndex) End Sub Private Sub DataGridView1_CellClick(ByVal sender As System.Object,ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick '在第一行显示comBox If Me.DataGridView1.CurrentCell.ColumnIndex = 0 Then '确定位置,显示comBox Dim rect As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(Me.DataGridView1.CurrentCell.ColumnIndex,Me.DataGridView1.CurrentCell.RowIndex,False) cmb.Left = rect.Left cmb.Top = rect.Top cmb.Width = rect.Width cmb.Height = rect.Height cmb.Visible = True Else cmb.Visible = False End If End Sub Function NewTB() As DataTable Dim tb As New DataTable tb.Columns.Add("序号") tb.Columns.Add("名称") tb.Columns.Add("类型") tb.Rows.Add("1","aaa","biggggggggggsssssss") tb.Rows.Add("2","bbb","smlllllllllllll") tb.Rows.Add("3","ccc","dogsssssssssssssssss") Return tb End Function Private Sub Button3_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button3.Click My.Forms.Form2.Show() End Sub End Class
正式使用这个控件过程发现:多列现实的CombBox,数据量太大显示慢。每次点击combBox都要刷新网格,影响编辑效率,改回用其他方法。
如果数据量小。还是可以用这个控件的。