none
change all column value of datagridview RRS feed

  • Question

  • guys help me on how to update a column

    i have a data here

    price        paid         balance

    3              0               12

    4              0               12

    5              0               12

    what i want to do is to update the "paid" column and after clicking a button my DGV will become like this

    price        paid         balance

    3              12               0

    4              12               0

    5              12               0

    i have this code but i can only change the first row please help me

      Dim i = DataGridView1.CurrentRow.Index

            With DataGridView1
                .Rows(i).Cells("paid").Value = txt_bal.Text

            End With

    Saturday, November 17, 2018 8:41 AM

Answers

  • Try this

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For Each row As DataGridViewRow In DataGridView1.Rows
                If Not row.IsNewRow Then
                    row.Cells(1).Value = txt_bal.Text
                End If
            Next
        End Sub
    End Class

    And 

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            For Each row As DataGridViewRow In DataGridView1.Rows
                If Not row.IsNewRow Then
                    row.Cells(1).Value = txt_bal.Text
                    row.Cells(2).Value = 0
                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



    Saturday, November 17, 2018 9:28 AM
    Moderator

All replies

  • Try this

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For Each row As DataGridViewRow In DataGridView1.Rows
                If Not row.IsNewRow Then
                    row.Cells(1).Value = txt_bal.Text
                End If
            Next
        End Sub
    End Class

    And 

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            For Each row As DataGridViewRow In DataGridView1.Rows
                If Not row.IsNewRow Then
                    row.Cells(1).Value = txt_bal.Text
                    row.Cells(2).Value = 0
                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



    Saturday, November 17, 2018 9:28 AM
    Moderator
  • Hi,

    try the code:

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click       
            DataGridView1.Columns(1).DataPropertyName = dt.Columns(2).ToString
            DataGridView1.Columns(2).DataPropertyName = dt.Columns(1).ToString
        End Sub

    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.

    Monday, November 19, 2018 7:55 AM