在 VB.NET Listview 中插入控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13654455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert Controls in VB.NET Listview
提问by OussamaLord
Hello I have a Listview in VB.NET and I want to insert inside it a name and a button as following: The name should be displayed in the left of the listview and the button in the right (just like if I have a row inside the listeview and the two controls placed in the left and right of that row) so, is it possible in VB.NET and if it's the case how? I hope my question was clear. Thanks.
您好,我在 VB.NET 中有一个 Listview,我想在其中插入一个名称和一个按钮,如下所示:名称应显示在列表视图的左侧,而按钮应显示在右侧(就像我在里面有一行一样listeview 和放置在该行左侧和右侧的两个控件)那么,是否可以在 VB.NET 中使用,如果是这种情况怎么办?我希望我的问题很清楚。谢谢。
回答by Steve
Not easily. However a DataGridView could handle this easily:
不容易。但是 DataGridView 可以轻松处理此问题:


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"})
These properties can be set from the designer, and you would probably bind the datagridview rather than add the rows manually.
这些属性可以从设计器中设置,您可能会绑定 datagridview 而不是手动添加行。
EDIT to remove a row on button click, use the CellContentClick event:
编辑以在按钮单击时删除一行,使用 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
Note if you do use databinding, you should remove the row from the data object and not the datagridview
请注意,如果您确实使用数据绑定,则应从数据对象中删除该行,而不是从 datagridview
EDIT Extracting all column on items to a list:
编辑将项目上的所有列提取到列表中:
Dim columnOneValues As New List(Of String)
For Each row As DataGridViewRow In DataGridView1.Rows
columnOneValues.Add(row.Cells(0).Value.ToString)
Next
回答by alldayremix
Short answer:no.
简短的回答:没有。
Longer answer:yes, but this is nontrivial, and you can't do it with the stock ListView.
更长的答案:是的,但这很重要,你不能用 stock 来做到这一点ListView。
You'd have to extend the class, handle the drawing of the controls yourself, calculating their positions and dimensions according to the size of the ListViewItem. Furthermore, if a user reorders the ListViewcolumns, there is no trivial way to get the new column order.
您必须扩展该类,自己处理控件的绘制,根据ListViewItem. 此外,如果用户对ListView列重新排序,则没有简单的方法来获得新的列顺序。
See this codeproject pagefor an example of a custom ListView.
请参见本CodeProject上页自定义的一个例子ListView。

