vb.net - 在 VB.NET ListView 中插入控件

标签 vb.net

您好,我在 VB.NET 中有一个 ListView ,我想在其中插入一个名称和一个按钮,如下所示:名称应显示在 ListView 的左侧,按钮显示在右侧(就像我有listeview 中的一行以及放置在该行左侧和右侧的两个控件)那么,在 VB.NET 中是否可能,如果是这种情况如何? 我希望我的问题很清楚。 谢谢。

最佳答案

不容易。然而 DataGridView 可以轻松处理这个问题:

enter image description here

    DataGridView1.Columns.AddRange(New DataGridViewColumn(1) _
                                    {New DataGridViewTextBoxColumn() With _
                                     {.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, .HeaderText = "Text"},
                                     New DataGridViewButtonColumn() With _
                                     {.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, .HeaderText = "Button"}})
    DataGridView1.Rows.Add({"row 1 text", "row 1 button"})
    DataGridView1.Rows.Add({"row 2 text", "row 2 button"})

这些属性可以从设计器中设置,您可能会绑定(bind) datagridview 而不是手动添加行。

编辑要在单击按钮时删除一行,请使用 CellContentClick 事件:

Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    'check it the button column being clicked, and check they are not clicking the column heading
    If e.ColumnIndex = 1 And e.RowIndex >= 0 Then
        DataGridView1.Rows.RemoveAt(e.RowIndex)
    End If
End Sub

请注意,如果您确实使用数据绑定(bind),则应该从数据对象中删除该行,而不是从 datagridview 中删除

编辑 将项目上的所有列提取到列表中:

 Dim columnOneValues As New List(Of String)

 For Each row As DataGridViewRow In DataGridView1.Rows
        columnOneValues.Add(row.Cells(0).Value.ToString)
 Next

关于vb.net - 在 VB.NET ListView 中插入控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13654455/

相关文章:

c# - Wpf 控制可见性?

.net - 使用 JSON.net 反序列化

mysql - 如果没有找到记录如何显示消息

c# - 在 ToolStripMenuItem 中捕获 mouseClick 和 KeyDown 事件

vb.net - "Object reference not set to an instance of an object"尝试添加到列表

c# - 单行 VB.NET 公共(public)属性

vb.net - 如何验证我的应用程序运行所在的事件目录域?

wpf - 单击数据绑定(bind) ComboBoxItem 不会更新父 ComboBox

mysql - 通过存储过程分配行号

vb.net - 避免文本框中存在现有项目