Solved

Select multiple columns of a datagridview in Vb.net

Posted on 2013-12-13
4
1,415 Views
Last Modified: 2014-01-07
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!
0
Comment
Question by:Karen Wilson
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39716845
You can select individual columns like:
            dataGridView1.Columns[0].Selected = true;
            dataGridView1.Columns[1].Selected = true;

Open in new window

But you will also have to make sure that the SelectionMode property is set to FullColumnSelect
0
 

Author Comment

by:Karen Wilson
ID: 39716879
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.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39755039
You can disable sorting when exporting to Excel and enable it again when done exporting.
0
 

Author Comment

by:Karen Wilson
ID: 39761861
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
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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