none
Copy a column in another one in the same dgv in vb.net 2017 RRS feed

  • Question

  • Hi everyone, I am newest in this field. So I would like to copy a column to another in the same dgv. I am using this code in order to get my goal. However i don't know what is wrong.

    Private Sub Updatetop()

            Dim top, base, length As Double

            For Each fila As DataGridViewRow In DGVBHA.Rows

                If Double.TryParse(CStr(fila.Cells(0).Value), top) = True AndAlso
                        Double.TryParse(CStr(fila.Cells(2).Value), length) = True Then
                    fila.Cells(1).Value = length + top

                    If Double.TryParse(CStr(fila.Cells(1).Value), base) = True Then

                    End If
                   For base = 0 To DGVBHA.Rows.Count - 1

                        If fila.Cells(0).Value = 0 Then

                            DGVBHA.Rows(base).Cells(0).Value = base

                        End If

                    Next

                Else
                    fila.Cells(1).Value = ""

                End If

            Next
        End Sub

    The picture below shows what I pretend to do.

    I need to copy the values of the column BOTTOM to column TOP from the cell number 2 I mean the first number in the cell has to stay zero (0).

    I really appreciate some advices from you guys, thanks in advance for supporting me.

    Wednesday, December 12, 2018 9:22 PM

Answers

  • Here is a roadmap to work with which copies the second column to the first column if a valid Double in the second column.

    Public Class Form1
        '
        ' DataGridView has two columns named Column1 and Column2
        '
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add(10, 20.3)
            DataGridView1.Rows.Add(11, 2.5)
            DataGridView1.Rows.Add(11, "X")
            DataGridView1.Rows.Add(1, 44.2)
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = DataGridView1.Rows.Cast(Of DataGridViewRow).
                    Where(Function(row) Not row.IsNewRow).
                    Select(Function(row, index) New With {.Index = index, .Value = row.Cells("Column2").Value.ToString()})
    
            Dim validDouble = 0.4
            For Each o In result
                If Double.TryParse(o.Value, validDouble) Then
                    DataGridView1.Rows(o.Index).Cells("Column1").Value = validDouble
                End If
            Next
    
        End Sub
    End Class


    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. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Thursday, December 13, 2018 1:03 AM
    Moderator
  • Hi,

    I think you can try my code to solve your problem.

    Imports System.Data.SqlClient
    Public Class Form1
        Dim dt As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add("Top", GetType(Double))
            dt.Columns.Add("Bottom", GetType(Double))
            dt.Columns.Add("xxxx", GetType(Double))
            dt.Rows.Add(New Object() {14, 29, 12})
            dt.Rows.Add(New Object() {10, 25, 14})
            dt.Rows.Add(New Object() {13, 21, 67})
            dt.Rows.Add(New Object() {15, 23, 81})
            dt.Rows.Add(New Object() {12, 26, 24})
            DGVBHA.DataSource = dt
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim i
            For i = 1 To DGVBHA.Rows.Count - 1
                DGVBHA.Rows(i).Cells("Top").Value = DGVBHA.Rows(i).Cells("Bottom").Value
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 13, 2018 1:53 AM

All replies

  • Here is a roadmap to work with which copies the second column to the first column if a valid Double in the second column.

    Public Class Form1
        '
        ' DataGridView has two columns named Column1 and Column2
        '
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add(10, 20.3)
            DataGridView1.Rows.Add(11, 2.5)
            DataGridView1.Rows.Add(11, "X")
            DataGridView1.Rows.Add(1, 44.2)
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = DataGridView1.Rows.Cast(Of DataGridViewRow).
                    Where(Function(row) Not row.IsNewRow).
                    Select(Function(row, index) New With {.Index = index, .Value = row.Cells("Column2").Value.ToString()})
    
            Dim validDouble = 0.4
            For Each o In result
                If Double.TryParse(o.Value, validDouble) Then
                    DataGridView1.Rows(o.Index).Cells("Column1").Value = validDouble
                End If
            Next
    
        End Sub
    End Class


    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. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Thursday, December 13, 2018 1:03 AM
    Moderator
  • Hi,

    I think you can try my code to solve your problem.

    Imports System.Data.SqlClient
    Public Class Form1
        Dim dt As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add("Top", GetType(Double))
            dt.Columns.Add("Bottom", GetType(Double))
            dt.Columns.Add("xxxx", GetType(Double))
            dt.Rows.Add(New Object() {14, 29, 12})
            dt.Rows.Add(New Object() {10, 25, 14})
            dt.Rows.Add(New Object() {13, 21, 67})
            dt.Rows.Add(New Object() {15, 23, 81})
            dt.Rows.Add(New Object() {12, 26, 24})
            DGVBHA.DataSource = dt
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim i
            For i = 1 To DGVBHA.Rows.Count - 1
                DGVBHA.Rows(i).Cells("Top").Value = DGVBHA.Rows(i).Cells("Bottom").Value
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 13, 2018 1:53 AM
  • Thank you Karen Payne for your reply
    Thursday, December 13, 2018 1:57 AM
  • Thank you Karen Payne for your reply
    Your welcome :-)

    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. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 13, 2018 1:59 AM
    Moderator
  • Thank you Alex for your help.
    Thursday, December 13, 2018 1:59 AM