Solved

Select multiple columns of a datagridview in Vb.net

Posted on 2013-12-13
4
1,356 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
Comment Utility
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
Comment Utility
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
Comment Utility
You can disable sorting when exporting to Excel and enable it again when done exporting.
0
 

Author Comment

by:Karen Wilson
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now