Datagridview vb.net中的组合框
Posted
技术标签:
【中文标题】Datagridview vb.net中的组合框【英文标题】:Combobox in Datagrid view vb.net 【发布时间】:2016-03-31 19:15:13 【问题描述】:我知道这个问题太老了,被问了很多次,但我没有找到我想要的确切内容。 我曾经有一些组合框和文本框,用户将填写并选择数据,然后在最后一个框上按回车键,所有数据都作为新行添加到数据网格中。 现在我想要的是让我的数据网格中的第一行替换所有这些框。 但我已经卡在第一个项目上。
我希望第一列是一个组合框,但它会根据我编写的查询从 SQL 数据库中获取其值。在组合框中,我想输入我想要搜索和选择的内容(作为普通组合框中的下拉样式)。
类似于下面的代码,但用于 datagridview 中的组合框。
Dim sqlquery = " SELECT m.accno,m.accname
FROM masteraccount m
where accname like '%TextBox3.Text%'
or accno like '%TextBox3.Text%' "
Using comm As SqlCommand = New SqlCommand(sqlquery, SQL.SQLCon)
Dim rs As SqlDataReader = comm.ExecuteReader
Dim dt As DataTable = New DataTable
dt.Load(rs)
ListBox1.DataSource = dt
ComboBox1.DisplayMember = "acc"
ComboBox1.ValueMember = "acc"
End Using
【问题讨论】:
您必须添加一列作为组合框。我相信您可以在 ***.com 上找到足够的示例 是的,有,但没有人展示我如何能够完全按照我的意愿处理 sql 关系。 【参考方案1】:我根据您的要求更改了代码。当您从组合框中键入或选择一个项目时,它会填充其他单元格。两种方法具有相同的功能。检查下面的修改代码。
Private Sub FillComboBox()
If cn.State = ConnectionState.Open Then cn.Close()
cn.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Juan\Documents\NorthwindSample.accdb;Persist Security Info=False;"
cn.Open()
cmb.Name = "cmb"
cmb.HeaderText = "Employee"
'query that retrieves all the employee names
Dim sqlquery As String = "Select [last name] & ' ' & [first name] as Name " &
"From employees order by [last name]"
'add the query results to the combobox cell in datagridview
Using comm As OleDbCommand = New OleDbCommand(sqlquery, cn)
Dim rs As OleDbDataReader = comm.ExecuteReader
dtable = New DataTable
dtable.Load(rs)
cmb.DataSource = dtable
cmb.DisplayMember = dtable.Columns.Item(0).ColumnName
cmb.ValueMember = dtable.Columns.Item(0).ColumnName
DataGridView1.Columns.Add(cmb)
End Using
'create other columns
CreateColumns()
End Sub
'Procedure to fill the other cells
Sub FillCells(cmbValue As String, conn As OleDbConnection)
'query that retrieves the rest of the fields that matches with the combobox value
Dim sqlquery2 As String = "Select [Last Name] & ' ' & [First Name] as Name, [e-mail address], [job title], city
From employees
Where [Last Name] & ' ' & [First Name] = '" & cmbValue & "'"
'Assigns query results to datatable
Using comm2 As OleDbCommand = New OleDbCommand(sqlquery2, conn)
Dim rs As OleDbDataReader = comm2.ExecuteReader
Dim dt As DataTable = New DataTable
dt.Load(rs)
'creates a datarow for the datatable
Dim dtRow As DataRow
Dim email As String = ""
Dim jobtitle As String = ""
Dim city As String = ""
'assigns datarow items to variables
For Each dtRow In dt.Rows
dtRow.Field(Of String)(dt.Columns.Item(1))
email = dtRow.Item(1)
jobtitle = dtRow.Item(2)
city = dtRow.Item(3)
Next
'assigns the index of the current row to the variable
Dim currentRow As Integer
currentRow = DataGridView1.CurrentRow.Index
'fills the cells with the values
DataGridView1.Item(1, currentRow).Value = email
DataGridView1.Item(2, currentRow).Value = jobtitle
DataGridView1.Item(3, currentRow).Value = city
dt = Nothing
End Using
End Sub
Private Sub DataGridView1_EditingControlShowing(sender As Object,
e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
'REMEMBER TO CHANGE THE COLUMN INDEX NUMBER TO YOUR COMBOBOX INDEX!!
If DataGridView1.CurrentCell.ColumnIndex = 0 Then
Dim combo As ComboBox = CType(e.Control, ComboBox)
combo.DropDownStyle = ComboBoxStyle.DropDown
If (combo IsNot Nothing) Then
' Remove an existing event-handler, if present, to avoid adding multiple handlers when the editing control is reused.
RemoveHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
RemoveHandler combo.TextChanged, New EventHandler(AddressOf ComboBox_TextChanged)
RemoveHandler combo.KeyDown, New KeyEventHandler(AddressOf ComboBox_KeyDown)
' Add the event handler.
AddHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
AddHandler combo.TextChanged, New EventHandler(AddressOf ComboBox_TextChanged)
AddHandler combo.KeyDown, New KeyEventHandler(AddressOf ComboBox_KeyDown)
End If
End If
End Sub
Private Sub ComboBox_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim combo As ComboBox = CType(sender, ComboBox)
FillCells(combo.Text, cn)
End Sub
Private Sub ComboBox_TextChanged(sender As Object,
e As EventArgs)
Dim combo As ComboBox = CType(sender, ComboBox)
FillCells(combo.Text, cn)
End Sub
Private Sub ComboBox_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
Select Case (e.KeyCode)
Case Keys.Tab
e.Equals(Keys.Return)
End Select
End Sub
【讨论】:
感谢它比其他代码工作得更好,但仍然无法搜索组合框。如果您有 1000 名员工,我想根据我输入的内容显示他们(检索所有员工姓名的查询 Dim sqlquery As String = "Select [last name] and [first name] where [firstname] like ' combobox.text % '"以上是关于Datagridview vb.net中的组合框的主要内容,如果未能解决你的问题,请参考以下文章
用 subsonic & vb.net 填充 datagridview 组合框列
是否可以显示 MySQL 服务器的数据库,将其放入组合框中,并将其内容显示在 VB.Net 的 datagridview 表中?
在 vb.net 的文本框中显示 datagridview 单元格数据