Solved

Select multiple columns of a datagridview in Vb.net

Posted on 2013-12-13
4
1,473 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
[X]
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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

695 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