locked
Delete space after text in datagridview RRS feed

  • Question

  • Is there any way to delete the space after the last string of text in a datagridview cell?  For example, if the user types "My name is John." and then a few spaces, I want to delete those few spaces through code.  The reason is because I'm exporting some of the cell contents to a CSV file, and I don't want the spaces to show up.  I found some code for textboxes, but haven't found anything for datagridview cells.  Thanks. 

    tbPositionCursor.Select(tbPositionCursor.Text.Length, 0)


    • Edited by Luna1200 Monday, January 27, 2014 7:19 PM mistake
    Monday, January 27, 2014 7:19 PM

Answers

  • Thanks for the example, however, it still doesn't seem to be working.  I've tried:

    For Each dgRow As DataGridViewRow In DataGridView1.Rows
                If dgRow.Selected = False Then
                    Trim(dgRow.Cells(1).Value)
                    Trim(dgRow.Cells(4).Value)
                End If
            Next

    Trim() is a function that returns a string.  You need to do something with the return value.

                    dgRow.Cells(4).Value = Trim(dgRow.Cells(4).Value.ToString)

    • Edited by Acamar Monday, January 27, 2014 9:41 PM sp
    • Marked as answer by Luna1200 Tuesday, January 28, 2014 3:29 AM
    Monday, January 27, 2014 9:37 PM

All replies

  • TRIM works great for that!

    Chuck

    Monday, January 27, 2014 7:25 PM
  • Hi Luna, you can try something like:

    Me.DataGridViewName.CurrentRow.Cells("field_name").Value.ToString.TrimEnd

    or

    Me.DataGridViewName.CurrentRow.Cells("field_name").Value.ToString.Trim

    Monday, January 27, 2014 7:58 PM
  • Thanks for the reply.  I can't seem to get the trim to work.  I've tried:

    For Each dgRow As DataGridViewRow In DataGridView1.Rows
                If dgRow.Selected = False Then
                    Trim(dgRow.Cells(1).Value)
                    Trim(dgRow.Cells(4).Value)
                End If
            Next

    and

    For Each dgRow As DataGridViewRow In DataGridView1.Rows
                If dgRow.Selected = False Then
                    dgRow.Cells(1).Value.ToString.Trim()
                    dgRow.Cells(4).Value.ToString.Trim()
                End If
            Next
    


    Monday, January 27, 2014 8:43 PM
  • Thanks for the example, however, it still doesn't seem to be working.  I've tried:

    For Each dgRow As DataGridViewRow In DataGridView1.Rows
                If dgRow.Selected = False Then
                    Trim(dgRow.Cells(1).Value)
                    Trim(dgRow.Cells(4).Value)
                End If
            Next
    

    and

    For Each dgRow As DataGridViewRow In DataGridView1.Rows
                If dgRow.Selected = False Then
                    dgRow.Cells(1).Value.ToString.Trim()
                    dgRow.Cells(4).Value.ToString.Trim()
                End If
            Next
    

    Monday, January 27, 2014 8:45 PM
  • Thanks for the example, however, it still doesn't seem to be working.  I've tried:

    For Each dgRow As DataGridViewRow In DataGridView1.Rows
                If dgRow.Selected = False Then
                    Trim(dgRow.Cells(1).Value)
                    Trim(dgRow.Cells(4).Value)
                End If
            Next

    Trim() is a function that returns a string.  You need to do something with the return value.

                    dgRow.Cells(4).Value = Trim(dgRow.Cells(4).Value.ToString)

    • Edited by Acamar Monday, January 27, 2014 9:41 PM sp
    • Marked as answer by Luna1200 Tuesday, January 28, 2014 3:29 AM
    Monday, January 27, 2014 9:37 PM
  • Hello,

    If you want to get rid of spaces and export to CSV try the following where I created four columns in the IDE for the DataGridView.

    Form code

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add(
                New Object() {"Mary  ", "Smith", 10, Now.ToShortDateString})
            DataGridView1.Rows.Add(
                New Object() {"  Bill  ", " Smith", Nothing, Now.ToShortDateString})
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            DataGridView1.ExportRows(
                IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test.csv"))
        End Sub
    End Class

    Add a code module, in this case Module1.vb

    Module Module1
        <System.Diagnostics.DebuggerStepThrough()> _
        <Runtime.CompilerServices.Extension()> _
        Public Sub ExportRows(ByVal sender As DataGridView, ByVal FileName As String)
            Dim Lines =
                (
                    From row In sender.Rows
                    Where Not DirectCast(row, DataGridViewRow).IsNewRow
                    Let RowItem = String.Join(",", Array.ConvertAll(
                                              DirectCast(row, DataGridViewRow).Cells.Cast(Of DataGridViewCell).ToArray, _
                                                                    Function(c As DataGridViewCell) _
                                                                        If(c.Value Is Nothing, "", CStr(c.Value).Trim)))
                    Select RowItem
                ).ToArray
            IO.File.WriteAllLines(FileName, Lines)
        End Sub
    End Module
    

    Screenshot of DataGridView after load

    Test.csv

    Mary,Smith,10,1/27/2014
    Bill,Smith,,1/27/2014
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, January 27, 2014 10:27 PM
  • This worked nicely Acamar.  Thanks!

    • Edited by Luna1200 Tuesday, January 28, 2014 3:31 AM
    Tuesday, January 28, 2014 3:29 AM