日期:2014-05-18  浏览次数:20853 次

如何在ListView单元中使用combobox进行编辑?
我用listview做的一个表格,其中一列要求是数据以combobox的形式显示和编辑,C#中怎么用代码来实现?
在线等!

------解决方案--------------------
我这段代码是实现 DataGridVIew控件单元格显示ComboBox的vb程序,不知道对LZ来说有没有参考价值。

VB.NET code

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

------解决方案--------------------
2楼的差不多吧,就是要动态地去计算一下用户鼠标点击的位置位于当前哪一行哪一列,再计算出该单元格的位置及大小,然后显示出来~
------解决方案--------------------
系统自带的listview没有这个功能,建议LZ可以用datagridview,baidu一下,有很多的