none
copy column and format RRS feed

  • Question

  • hi

    Use the following code to copy columns from a DataGridView  to another
    The code works well
    If there is no empty cell in the Time or Date field

    field 3 date with format  "MM/dd/yyyy"

    field 4 time format  "hh:mm:ss tt"

    field5 time  format  "hh:mm:ss tt"



    But the problem may be blank fields for time or date then the code does not work
    What is required is to transfer and copy data from the DataGridView 1 to the second and ignore the empty or null values date and time

       For i = 0 To 5
                FEMPLOYPRINTEND.DATAGID_PRINT.Columns.Add(DATAG_EMPLOYEDET.Columns(i).Clone)
            Next
    
            For ii = 0 To DATAG_EMPLOYEDET.Rows.Count - 1
                Dim col0 = DATAG_EMPLOYEDET.Rows(ii).Cells(0).Value
                Dim col1 = DATAG_EMPLOYEDET.Rows(ii).Cells(1).Value
                Dim col2 = DATAG_EMPLOYEDET.Rows(ii).Cells(2).Value
                Dim col3 = Format(DATAG_EMPLOYEDET.Rows(ii).Cells(3).Value, "MM/dd")
                Dim col4 = Format(DATAG_EMPLOYEDET.Rows(ii).Cells(4).Value, "hh:mm tt")
                Dim col5 = Format(DATAG_EMPLOYEDET.Rows(ii).Cells(5).Value, "hh:mm tt")
    
                FEMPLOYPRINTEND.DATAGID_PRINT.Rows.Add(col0, col1, col2, col3, col4, col5)
            Next

    my try not work

     For i = 0 To 5
                FEMPLOYPRINTEND.DATAGID_PRINT.Columns.Add(DATAG_EMPLOYEDET.Columns(i).Clone)
            Next
    
            For ii = 0 To DATAG_EMPLOYEDET.Rows.Count - 1
                Dim col0 = DATAG_EMPLOYEDET.Rows(ii).Cells(0).Value
                Dim col1 = DATAG_EMPLOYEDET.Rows(ii).Cells(1).Value
                Dim col2 = DATAG_EMPLOYEDET.Rows(ii).Cells(2).Value
                Dim col3
                Dim col4
                If Not DATAG_EMPLOYEDET.Rows(ii).Cells(3).Value = Nothing Then
                    col3 = Format(DATAG_EMPLOYEDET.Rows(ii).Cells(3).Value, "MM/dd/yyyy")
                Else
                    col3 = DATAG_EMPLOYEDET.Rows(ii).Cells(3).Value
                End If
                If Not DATAG_EMPLOYEDET.Rows(ii).Cells(4).Value = Nothing Then
                    col4 = Format(DATAG_EMPLOYEDET.Rows(ii).Cells(4).Value, "hh:mm:ss tt")
                Else
                    col4 = DATAG_EMPLOYEDET.Rows(ii).Cells(4).Value
                End If
    
                Dim col5
                If Not DATAG_EMPLOYEDET.Rows(ii).Cells(5).Value = Nothing Then
                    col5 = Format(DATAG_EMPLOYEDET.Rows(ii).Cells(5).Value, "hh:mm:ss tt")
                Else
                    col5 = DATAG_EMPLOYEDET.Rows(ii).Cells(5).Value
                End If
    
               
                FEMPLOYPRINTEND.DATAGID_PRINT.Rows.Add(col0, col1, col2, col3, col4, col5)
            Next

    Thursday, March 28, 2019 8:21 PM

Answers

  • See if these language extensions help. First one copies a single row to another DataGridView meant to be used in a for/each or for/next. The second one clones columns from one DataGridView to another DataGridView.

    Example found here

    Module DataGridViewExtensions
        ''' <summary>
        ''' Used to copy a row from one DataGridView to another DataGridView
        ''' </summary>
        ''' <param name="SingleRow"></param>
        ''' <param name="Target"></param>
        ''' <remarks>
        ''' Does not handle multiple rows selected
        ''' </remarks>
        <System.Diagnostics.DebuggerStepThrough()> _
        <System.Runtime.CompilerServices.Extension()> _
        Public Sub CloneRowWithValues(ByVal SingleRow As DataGridViewRow, ByVal Target As DataGridView)
    
            Dim Results As DataGridViewRow = CType(SingleRow.Clone(), DataGridViewRow)
    
            For Row As Int32 = 0 To SingleRow.Cells.Count - 1
                Results.Cells(Row).Value = SingleRow.Cells(Row).Value
            Next
    
            Target.Rows.Add(Results)
    
        End Sub
        ''' <summary>
        ''' Used to copy columns from another DataGridView to this DataGridView
        ''' </summary>
        ''' <param name="Self"></param>
        ''' <param name="CloneFrom"></param>
        ''' <remarks>
        ''' Only does cloning if Self has no columns
        ''' </remarks>
        <Runtime.CompilerServices.Extension()> _
        Public Sub CloneColumns(ByVal Self As DataGridView, ByVal CloneFrom As DataGridView)
    
            If Self.ColumnCount = 0 Then
                For Each c As DataGridViewColumn In CloneFrom.Columns
                    Self.Columns.Add(CType(c.Clone, DataGridViewColumn))
                Next
            End If
    
        End Sub
    End Module


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, March 28, 2019 10:20 PM
    Moderator

All replies

  • Hi

    Please show an Image of the master DGV, and an Image of required copied DGV - show with empty cells too.


    Regards Les, Livingston, Scotland

    Thursday, March 28, 2019 9:04 PM
  • See if these language extensions help. First one copies a single row to another DataGridView meant to be used in a for/each or for/next. The second one clones columns from one DataGridView to another DataGridView.

    Example found here

    Module DataGridViewExtensions
        ''' <summary>
        ''' Used to copy a row from one DataGridView to another DataGridView
        ''' </summary>
        ''' <param name="SingleRow"></param>
        ''' <param name="Target"></param>
        ''' <remarks>
        ''' Does not handle multiple rows selected
        ''' </remarks>
        <System.Diagnostics.DebuggerStepThrough()> _
        <System.Runtime.CompilerServices.Extension()> _
        Public Sub CloneRowWithValues(ByVal SingleRow As DataGridViewRow, ByVal Target As DataGridView)
    
            Dim Results As DataGridViewRow = CType(SingleRow.Clone(), DataGridViewRow)
    
            For Row As Int32 = 0 To SingleRow.Cells.Count - 1
                Results.Cells(Row).Value = SingleRow.Cells(Row).Value
            Next
    
            Target.Rows.Add(Results)
    
        End Sub
        ''' <summary>
        ''' Used to copy columns from another DataGridView to this DataGridView
        ''' </summary>
        ''' <param name="Self"></param>
        ''' <param name="CloneFrom"></param>
        ''' <remarks>
        ''' Only does cloning if Self has no columns
        ''' </remarks>
        <Runtime.CompilerServices.Extension()> _
        Public Sub CloneColumns(ByVal Self As DataGridView, ByVal CloneFrom As DataGridView)
    
            If Self.ColumnCount = 0 Then
                For Each c As DataGridViewColumn In CloneFrom.Columns
                    Self.Columns.Add(CType(c.Clone, DataGridViewColumn))
                Next
            End If
    
        End Sub
    End Module


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, March 28, 2019 10:20 PM
    Moderator