Link to home
Start Free TrialLog in
Avatar of Karen Wilson
Karen WilsonFlag for United States of America

asked on

Select multiple columns of a datagridview in Vb.net

I usually use the datagridview.SelectAll() and copy/paste the grid to Excel.  Today, I just want to copy/paste the first two columns of the datagridview.  How do I do this?  

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Karen Wilson

ASKER

I tried to change selectionmode to full column select and it said that I had to change all the columns in my grid from automatic to something else.  So I did that and this code works.  The problem is, my users need the ability to sort the columns so I guess I program the columns to sort asc/dsc when they get clicked.
You can disable sorting when exporting to Excel and enable it again when done exporting.
What I finally did was approach it from a different angle and used Note Pad.  Below is my code.

Dim getDoc = (From id In d.tblAddBooks _
                                Where id.Christmas = "Yes" _
                                And id.Status = "Active" _
                                Order By id.People Ascending, id.StartDate Descending _
                                Select id).ToList

        If getDoc.Count > 0 Then

            Dim pr As Process = New Process

            Dim docString As New StringBuilder()

            For i As Integer = 0 To getDoc.Count - 1

                Dim n As String = CStr(getDoc.Item(i).People)
                Dim s As String = CStr(getDoc.Item(i).Street)
                Dim cS As String = CStr(getDoc.Item(i).City_State)
                Dim z As String = CStr(getDoc.Item(i).Zip)
                Dim ph As String = CStr(getDoc.Item(i).homePhone)

                docString.Append(n & " - " & s & " - " & cS & " " & z & vbCrLf)
            Next

            pr.StartInfo.FileName = "notepad.exe"
            pr.Start()
            pr.WaitForInputIdle()

            SendKeys.Send(docString.ToString)
        Else
            MsgBox("There are no records.", MsgBoxStyle.OkOnly, "Send to Note Pad")
        End If