在数据网格视图的单元格中添加Listview项

在数据网格视图的单元格中添加Listview项,listview,datagridview,corresponding-records,Listview,Datagridview,Corresponding Records,我在form1中有Listview列AccountID、AccountName和AccountType和Form2Datagridview未绑定的列是日期、DocNo AccountID、AccountName、借方、贷方 我需要两样东西: 当我在listview中选择任意一行并双击时,DatagridView的AccountID单元格中只应传输AccountID,并将AccountID作为查找值,相应的AccountName也应显示在Datgirdview的AccountName列中 如果我

我在
form1
中有
Listview
AccountID、AccountName
AccountType
Form2
Datagridview
未绑定的列是
日期、DocNo AccountID、AccountName、借方、贷方

我需要两样东西:

  • 当我在
    listview
    中选择任意一行并双击时,DatagridView的
    AccountID
    单元格中只应传输
    AccountID
    ,并将
    AccountID
    作为查找值,相应的
    AccountName
    也应显示在
    Datgirdview
    AccountName
    列中

  • 如果我在
    DataGridView
    AccountID
    单元格中手动输入
    AccountID
    ,相应的
    AccountName
    也应显示在
    datgridview
    AccountName
    列中


  • 请帮我解决这个问题。

    首先,你没有用语言和框架标记你的文章,也没有提到它。我正在发布VB.NET winform解决方案

  • 双击
    列表视图1上的
  • DataGridView
    中手动编辑:
  • Private子dgRuns_CellEndEdit(发送方作为对象,e作为DataGridViewCellEventArgs)处理dgRuns.CellEndEdit
    如果e.ColumnIndex=0,则“仅当编辑了AccountID列时”
    Me.ListView1.SelectedItems.Clear()
    'Me.ListView1.Items().Selected=True
    对于Me.ListView1.Items中的每个il,在列AccountID(索引0)中查找选定值
    如果il.subitem(0)=Me.dGrans.Rows(e.RowIndex).Cells(“AccountID”).Value,则在找到特定AccountID时为'
    
    il.focused=True'谢谢您的回复。你的假设是正确的,我需要vb.net。检查代码后,将向您提供反馈。您的第一个代码工作正常,但第二个代码不工作。没有出现错误,但没有显示相应的帐户。@TalatFarooq从技术上讲,它正在选择项目(如果您查询
    ListView1
    的选择,您可以进行检查)。但实际上,
    ListView
    还有一个奇怪之处,当焦点丢失时,它不会显示某个项目被选中。如果您单击,它将松开所选项目。我通过选择并聚焦
    列表视图1
    本身解决了这个问题。我马上修改我的答案。兄弟,它仍然不起作用。但不知何故,我通过直接从数据库而不是listview获取数据找到了解决方案。但这是因为你的帮助才得以实现的。谢谢你的帮助。
    Private Sub ListView1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles ListView1.MouseDoubleClick
        ' provided the ValueMember of this ListView is set to AccountID
        If Me.DataGridView1.SelectedRows.Count > 0 AndAlso Me.ListView1.SelectedItems.Count > 0 Then
            Me.DataGridView1.SelectedRows(0).Cells("AccountID").Value = CInt(Me.ListView1.SelectedItems(0).SubItems(0).Text)
        End If
    End Sub
    
    Private Sub dgRuns_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgRuns.CellEndEdit
        If e.ColumnIndex = 0 Then   ' Only if AccountID column is edited
            Me.ListView1.SelectedItems.Clear()
            'Me.ListView1.Items().Selected = True
            For Each il In Me.ListView1.Items   ' look for selected value in column AccountID (of index 0)
                If il.subitem(0) = Me.dgRuns.Rows(e.RowIndex).Cells("AccountID").Value Then  ' when found particular AccountID
                    il.focused = True    ' <<<<<<< focus on selected item
                    il.selected = True   ' set it as selected 
                    ListView1.Focus()    ' <<<<<<< focus on ListView1
                    ListView1.Select()   ' <<<<<<< select ListView1
                    Exit For             ' ...and exit the loopo
                End If
            Next il
        End If
    End Sub