如何在ListView单元中使用combobox进行编辑?

rack79 2008-07-21 10:12:16
我用listview做的一个表格,其中一列要求是数据以combobox的形式显示和编辑,C#中怎么用代码来实现?
在线等!
...全文
41 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
冷月孤峰 2008-07-21
系统自带的listview没有这个功能,建议LZ可以用datagridview,baidu一下,有很多的
回复
GentleCat 2008-07-21
2楼的差不多吧,就是要动态地去计算一下用户鼠标点击的位置位于当前哪一行哪一列,再计算出该单元格的位置及大小,然后显示出来~
回复
wzuomin 2008-07-21
我这段代码是实现 DataGridVIew控件单元格显示ComboBox的vb程序,不知道对LZ来说有没有参考价值。


Public Class Form1

Private DataGridView1 As New DataGridView
Private ComboBox1 As New ComboBox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.CellClick, AddressOf DataGridView1_CellClick
AddHandler ComboBox1.TextChanged, AddressOf ComboBox1_TextChanged

Me.DataGridView1.AllowUserToResizeRows = False
Me.DataGridView1.AllowUserToResizeColumns = False
Me.DataGridView1.Dock = DockStyle.Fill

Me.DataGridView1.Columns.Add("Column1", "Column1")
Me.DataGridView1.Columns.Add("Column2", "Column2")

For i As Integer = 1 To 3
Me.DataGridView1.Rows.Add("Row" + i.ToString + " Column1", "Row" + i.ToString + " Column2")
Next

For i As Integer = 0 To 10
Me.ComboBox1.Items.Add("Item" + i.ToString)
Next
Me.ComboBox1.Visible = False

Me.Controls.Add(ComboBox1)
Me.Controls.Add(DataGridView1)
End Sub

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
If e.ColumnIndex = 1 Then
Dim p As Point = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
p.Offset(Me.DataGridView1.Left, Me.DataGridView1.Top)
Me.ComboBox1.Location = p
Me.ComboBox1.Size = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Size
Me.ComboBox1.Visible = True
Else
Me.ComboBox1.Visible = False
End If
End Sub

Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Me.DataGridView1.CurrentCell.Value = Me.ComboBox1.Text
Me.ComboBox1.Visible = False
End Sub

End Class

回复
相关推荐
发帖
C#
创建于2007-09-28

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2008-07-21 10:12
社区公告

让您成为最强悍的C#开发者