Copying DataGridView Cells to an Excel spreadsheet using Control-C

Posted on 2013-12-09
Last Modified: 2013-12-15
I have a VB.Net app that contains a DataGridView. I need to incorporate functionality that allows the User to click in a specific cell and then drag the cursor from that cell to wherever they wish within the grid, selecting each cell within the range of the two points as it is being dragged. Once all the desired cells are selected, User can press "CTRL-C" to copy the data to an Excel Spreadsheet.
Question by:BlakeMcKenna
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 96

Expert Comment

by:Bob Learned
ID: 39708226
What have you tried, and what can I help you with?

Author Comment

ID: 39708638
Well, after looking at the situation a little further, I tried a test by dropping a new DGV onto a form and populating it with rows and columns. I tried to drag and move the cursor over the rows/columns and it worked just like in Excel. So, there is obviously something wrong with the way I have configured my dgv...just not sure what it would be. Do you know of any settings that would prevent it from allowing the dragging and selecting of cells? Here are the events below that I am currently using within my app.

        Private Sub dgvLoadTests_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dgvLoadTests.CellValidating
            EH.strRetVal = ""

            If dgvLoadTests.Item(e.ColumnIndex, e.RowIndex).IsInEditMode Then
                If Not dgvLoadTests.Item(e.ColumnIndex, e.RowIndex).ValueType Is GetType(Decimal) Then
                    Dim c As Control = dgvLoadTests.EditingControl
                    Dim cell As DataGridViewCell = dgvLoadTests.CurrentCell

                    If Not Decimal.TryParse(c.Text, Nothing) Then
                        If c.Text = "" Then
                            EH.strRetVal = "Invalid Numeric Value!" & "~I"
                            e.Cancel = True
                        End If
                        cell.Value = Format(c.Text, GetDecimalPlaces(c.Text))
                    End If
                End If
            End If

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_CellValidating() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.strRetVal)
    End Sub

    Private Sub dgvLoadTests_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvLoadTests.KeyDown
            EH.strRetVal = ""

            'Delete the value in the current cell
            If e.KeyCode = Keys.Delete Then
                Dim cell As DataGridViewCell = dgvLoadTests.CurrentCell
                cell.Value = ""
            End If

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_KeyDown() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.strRetVal)
    End Sub

Accepted Solution

BlakeMcKenna earned 0 total points
ID: 39708648
I just now figured it out. I had the "MultiSelect" property set to "False". I set it back to "True" and it worked like I wanted it to.

Author Closing Comment

ID: 39719712
I figured it out.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Import Data from Multiple Text Files in Excel 12 86
RLDC Reporting in Visual studio 11 43 sendkey 4 18
Create simple Add-in for Excel in Office 365 2 20
Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question