vb.net 如何设置 DataGridView 行标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13940806/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:35:52  来源:igfitidea点击:

How to Set DataGridView Row Tag

vb.netwinformsdatagridviewtags

提问by Alex

I am using a double click event on a listview that will add three columns to a datagridview. I'm not sure how to set the "Tag" property on the "selectedText" variable.

我在列表视图上使用双击事件,它将向数据网格视图添加三列。我不确定如何在“selectedText”变量上设置“Tag”属性。

Private Sub lwArticles_DoubleClick(sender As Object, e As System.EventArgs) Handles lwArticles.DoubleClick
    Dim selectedText = lwArticles.SelectedItems(0).SubItems.Item(0).Text 'Article No
    Dim selectedDesc = lwArticles.SelectedItems(0).SubItems.Item(1).Text 'Description
    Dim currRowNo As String = ""
    Dim alreadyExists = False

    For i As Integer = 0 To dgvDetail.Rows.Count - 1
        currRowNo = dgvDetail.Rows(i).Cells(0).Value
        If currRowNo = selectedText Then
            alreadyExists = True
            dgvDetail.Rows(i).Cells(2).Value += 1
            Exit For
        End If
    Next

    'If the entry doesn't exist, add it
    If Not alreadyExists Then
        dgvDetail.Rows.Add(New String() {selectedText, selectedDesc, 1})
    End If

End Sub

After this I loop through the row's tags to see the article numbers. It will be near my dgvDetail.Rows.Add() that I should be setting the Tag property to equal the selectedText ... Any one know how to do this?

在此之后,我遍历行的标签以查看文章编号。在我的 dgvDetail.Rows.Add() 附近,我应该将 Tag 属性设置为与 selectedText 相等......有人知道如何做到这一点吗?

Edit: The datagridview columns being populated are: "Article Number", "Description" and "Quantity". The quantity is set in the loop, basically if I've double clicked on the same thing twice, it will increment the third column (Cell(2)) by one.

编辑:填充的 datagridview 列是:“文章编号”、“描述”和“数量”。数量在循环中设置,基本上如果我双击同一件事两次,它会将第三列 (Cell(2)) 增加一。

回答by LarsTech

The Add "function" for the DataGridView control returns the index of the row in the grid, so you can try using that to reference the row:

DataGridView 控件的添加“函数”返回网格中行的索引,因此您可以尝试使用它来引用该行:

Dim rowIndex As Integer
rowIndex = dgvDetail.Rows.Add(New String() {selectedText, selectedDesc, 1})
dgvDetails.Rows(rowIndex).Tag = selectedText