none
context menu for columns datagridview RRS feed

Answers

  • Since in your post you are using a DataTable the following has a ContextMenu added at design time with one menu item named MyMenuItem.

    Double click the menu item and the Click event is generated, add code such as below.

    Private Sub MyMenuItem_Click(sender As Object, e As EventArgs) Handles MyMenuItem.Click
        ' code assumes there is data in the DataTable
        ' cast DataGridView.DataSource to the DataTable
        Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
    
        ' get all rows for the current column into a string delimited with a new line
        Dim results As String = String.Join(Environment.NewLine, dt.AsEnumerable _
            .Select(Function(row) CStr(row(DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name))).ToArray)
    
        ' set data to clipboard
        Clipboard.SetText(results)
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    hi karen

    this is worked , but i right click on 1 column copying all Columns row

    Do you want to copy a single cell or all cells in the column? I write it to copy all cells in a column.

    For the current cell

    Private Sub MyMenuItem_Click(sender As Object, e As EventArgs) Handles MyMenuItem.Click
    
        If DataGridView1.CurrentCell.Value IsNot Nothing Then
            Dim currentCellValue = DataGridView1.CurrentCell.Value.ToString
            Clipboard.SetText(currentCellValue)
        End If
    
    End Sub
    There are other ways that are more assertive yet let's keep it simple.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ja7adR Thursday, November 2, 2017 5:04 PM
    Thursday, November 2, 2017 5:03 PM
    Moderator

All replies

  • Since in your post you are using a DataTable the following has a ContextMenu added at design time with one menu item named MyMenuItem.

    Double click the menu item and the Click event is generated, add code such as below.

    Private Sub MyMenuItem_Click(sender As Object, e As EventArgs) Handles MyMenuItem.Click
        ' code assumes there is data in the DataTable
        ' cast DataGridView.DataSource to the DataTable
        Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
    
        ' get all rows for the current column into a string delimited with a new line
        Dim results As String = String.Join(Environment.NewLine, dt.AsEnumerable _
            .Select(Function(row) CStr(row(DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name))).ToArray)
    
        ' set data to clipboard
        Clipboard.SetText(results)
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 2, 2017 4:47 PM
    Moderator
  • Since in your post you are using a DataTable the following has a ContextMenu added at design time with one menu item named MyMenuItem.

    Double click the menu item and the Click event is generated, add code such as below.

    Private Sub MyMenuItem_Click(sender As Object, e As EventArgs) Handles MyMenuItem.Click
        ' code assumes there is data in the DataTable
        ' cast DataGridView.DataSource to the DataTable
        Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
    
        ' get all rows for the current column into a string delimited with a new line
        Dim results As String = String.Join(Environment.NewLine, dt.AsEnumerable _
            .Select(Function(row) CStr(row(DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name))).ToArray)
    
        ' set data to clipboard
        Clipboard.SetText(results)
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    hi karen

    this is worked , but i right click on 1 column copying all Columns row

    Thursday, November 2, 2017 4:56 PM
  • Since in your post you are using a DataTable the following has a ContextMenu added at design time with one menu item named MyMenuItem.

    Double click the menu item and the Click event is generated, add code such as below.

    Private Sub MyMenuItem_Click(sender As Object, e As EventArgs) Handles MyMenuItem.Click
        ' code assumes there is data in the DataTable
        ' cast DataGridView.DataSource to the DataTable
        Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
    
        ' get all rows for the current column into a string delimited with a new line
        Dim results As String = String.Join(Environment.NewLine, dt.AsEnumerable _
            .Select(Function(row) CStr(row(DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name))).ToArray)
    
        ' set data to clipboard
        Clipboard.SetText(results)
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    hi karen

    this is worked , but i right click on 1 column copying all Columns row

    Do you want to copy a single cell or all cells in the column? I write it to copy all cells in a column.

    For the current cell

    Private Sub MyMenuItem_Click(sender As Object, e As EventArgs) Handles MyMenuItem.Click
    
        If DataGridView1.CurrentCell.Value IsNot Nothing Then
            Dim currentCellValue = DataGridView1.CurrentCell.Value.ToString
            Clipboard.SetText(currentCellValue)
        End If
    
    End Sub
    There are other ways that are more assertive yet let's keep it simple.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ja7adR Thursday, November 2, 2017 5:04 PM
    Thursday, November 2, 2017 5:03 PM
    Moderator