Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DATAGRIDVIEW from excel

Posted on 2013-06-10
2
Medium Priority
?
312 Views
Last Modified: 2013-06-13
I am copy and paste data from excel into a datagridview with the following code

Dim strPasteText As String = Clipboard.GetText
        Dim strSplitter() As String = {vbCrLf}
        Dim strSplitterC() As String = {vbTab}
        Dim strRows() As String = strPasteText.Split(strSplitter, StringSplitOptions.RemoveEmptyEntries)
        Dim iRows As Integer = strRows.Length
        Dim strCells() As String = strRows(0).Split(strSplitterC, StringSplitOptions.None)
        Dim iCells As Integer = strCells.Length
        DataGridView1.RowCount = iRows
        DataGridView1.ColumnCount = iCells
        For iColCounter As Integer = 0 To iCells - 1
            DataGridView1.Columns(iColCounter).Name = strCells(iColCounter)
        Next
        DataGridView1.Rows(0).Selected = True
        For iRowCounter As Integer = 1 To iRows - 1
            strCells = strRows(iRowCounter).Split(strSplitterC, StringSplitOptions.None)
            For iCellCounter As Integer = 0 To iCells - 1
                DataGridView1.Rows(iRowCounter - 1).Cells(iCellCounter).Value = strCells(iCellCounter)
            Next
        Next
        Me.DataGridView1.Columns(0).Width = 90
        Me.DataGridView1.Columns(1).Width = 55
        Me.DataGridView1.Columns(2).Width = 55
        Me.DataGridView1.Columns(3).Width = 55
        Me.DataGridView2.Columns(0).Width = 90
        Me.DataGridView2.Columns(1).Width = 55
        Me.DataGridView2.Columns(2).Width = 40

Open in new window



this works fine, however I do not have control of the excel data and sometimes it comes with some extra characters  that I do not want on the datagridview, since once I get the data on the datagridview I am writting to SQL table, so I need this data to be clean.

What I have seen on this data coming is that sometimes has extra spaces or characters like  -  or "  . so is there any way to eliminate  all this once I copy into the datagridview  or before I copy
 there are not a lot of data posible 100 rows and 4 columns
0
Comment
Question by:teogos
1 Comment
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39238890
You can use trim function to get rid of any characters at either end of the string

strCells(iCellCounter).Trim().Trim("-")
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

783 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