none
change all column value of datagridview RRS feed

  • Question

  • is it possible to update all the value of a column in a datagridview in one click

    ex

    price        paid         balance

    3              0               3

    4              0               4

    5              0               5

    what i want to do is when i add data in my datagridview the balance above will update for example

    if the first data in DGV is     

     3              0               3

    and if i add 

    4              0               4

    my data in my datagridview will become like this

    3              0               7

    4              0               7

    is this possible?



    • Edited by no[one] Saturday, November 17, 2018 12:49 AM
    Friday, November 16, 2018 6:50 PM

Answers

  • no i just want to update the balance column, the first example is just a sample data what i want to input in my datagridview 

    if the first data in DGV is     

     3              0               3

    and if i  add this  in my DGV 

    4              0               4

    my  datagridview will become like this

    3              0               7

    4              0               7

    and if i add another

    5              0               5

    my  datagridview will become like this

    3              0               12

    4              0               12

    5              0               12

    sorry if my question is not clear


    As asked this will work via a DataTable is the simple approach which also ensures only numbers are entered.

    Public Class Form1
        Private dt As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Price", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Paid", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Balance", .DataType = GetType(Integer)})
            DataGridView1.DataSource = dt
    
            AddHandler dt.TableNewRow, AddressOf RowAdded
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
            AddHandler dt.RowDeleted, AddressOf RowDeleted
    
    
        End Sub
        Private Sub RowDeleted(sender As Object, e As DataRowChangeEventArgs)
            Updater()
        End Sub
        Private Sub ColumnChanged(sender As Object, e As DataColumnChangeEventArgs)
            Updater()
        End Sub
        Private Sub RowAdded(sender As Object, e As DataTableNewRowEventArgs)
            Updater()
        End Sub
        Private Sub Updater()
            RemoveHandler dt.TableNewRow, AddressOf RowAdded
            RemoveHandler dt.ColumnChanged, AddressOf ColumnChanged
            RemoveHandler dt.RowDeleted, AddressOf RowDeleted
    
            Dim result = dt.AsEnumerable().Select(Function(row) row.Field(Of Integer)("Price")).Sum()
    
            For Each row As DataRow In dt.Rows
                row.SetField(Of Integer)("Balance", result)
            Next
    
            AddHandler dt.TableNewRow, AddressOf RowAdded
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
            AddHandler dt.RowDeleted, AddressOf RowDeleted
        End Sub
    
        Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
            If e.Exception.Message = "Input string was not in a correct format." Then
                MessageBox.Show($"Invalid input for column {DataGridView1.Columns(e.ColumnIndex).Name}, correct or press ESC.")
            End If
        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

    • Marked as answer by no[one] Saturday, November 17, 2018 1:55 AM
    Saturday, November 17, 2018 1:38 AM
    Moderator
  • Here you go

    Public Class Form1
        Private dt As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Price", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Paid", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Balance", .DataType = GetType(Integer)})
            DataGridView1.DataSource = dt
    
            AddHandler dt.TableNewRow, AddressOf RowAdded
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
            AddHandler dt.RowDeleted, AddressOf RowDeleted
        End Sub
        Private Sub RowDeleted(sender As Object, e As DataRowChangeEventArgs)
            Updater()
        End Sub
        Private Sub ColumnChanged(sender As Object, e As DataColumnChangeEventArgs)
            Updater()
        End Sub
        Private Sub RowAdded(sender As Object, e As DataTableNewRowEventArgs)
            Updater()
        End Sub
        Private Sub Updater()
            RemoveHandler dt.TableNewRow, AddressOf RowAdded
            RemoveHandler dt.ColumnChanged, AddressOf ColumnChanged
            RemoveHandler dt.RowDeleted, AddressOf RowDeleted
    
            Dim result = dt.AsEnumerable().Select(Function(row) row.Field(Of Integer)("Price")).Sum()
    
            For Each row As DataRow In dt.Rows
                row.SetField(Of Integer)("Balance", result)
            Next
    
            AddHandler dt.TableNewRow, AddressOf RowAdded
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
            AddHandler dt.RowDeleted, AddressOf RowDeleted
        End Sub
    
        Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
            If e.Exception.Message = "Input string was not in a correct format." Then
                MessageBox.Show($"Invalid input for column {DataGridView1.Columns(e.ColumnIndex).Name}, correct or press ESC.")
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RemoveHandler dt.ColumnChanged, AddressOf ColumnChanged
            If dt.Rows.Count > 0 Then
                Dim balance = dt.Rows(0).Field(Of Integer)("Balance")
                For index As Integer = 0 To dt.Rows.Count - 1
                    dt.Rows(index).SetField(Of Integer)("Paid", balance)
                    dt.Rows(index).SetField(Of Integer)("Balance", 0)
                Next
            End If
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
        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

    • Marked as answer by no[one] Saturday, November 17, 2018 12:26 PM
    Saturday, November 17, 2018 10:32 AM
    Moderator

All replies

  • So does that include when the price column changes too?

    Can it be done? Sure but generally when someone wants something similar it's to do row totals and then grand totals but having the last column compute off, in this case the price column.

    The first example makes sense while the last example is rather awkward.


    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

    Friday, November 16, 2018 7:00 PM
    Moderator
  • no i just want to update the balance column, the first example is just a sample data what i want to input in my datagridview 

    if the first data in DGV is     

     3              0               3

    and if i  add this  in my DGV 

    4              0               4

    my  datagridview will become like this

    3              0               7

    4              0               7

    and if i add another

    5              0               5

    my  datagridview will become like this

    3              0               12

    4              0               12

    5              0               12

    sorry if my question is not clear


    Saturday, November 17, 2018 12:49 AM
  • no i just want to update the balance column, the first example is just a sample data what i want to input in my datagridview 

    if the first data in DGV is     

     3              0               3

    and if i  add this  in my DGV 

    4              0               4

    my  datagridview will become like this

    3              0               7

    4              0               7

    and if i add another

    5              0               5

    my  datagridview will become like this

    3              0               12

    4              0               12

    5              0               12

    sorry if my question is not clear


    As asked this will work via a DataTable is the simple approach which also ensures only numbers are entered.

    Public Class Form1
        Private dt As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Price", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Paid", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Balance", .DataType = GetType(Integer)})
            DataGridView1.DataSource = dt
    
            AddHandler dt.TableNewRow, AddressOf RowAdded
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
            AddHandler dt.RowDeleted, AddressOf RowDeleted
    
    
        End Sub
        Private Sub RowDeleted(sender As Object, e As DataRowChangeEventArgs)
            Updater()
        End Sub
        Private Sub ColumnChanged(sender As Object, e As DataColumnChangeEventArgs)
            Updater()
        End Sub
        Private Sub RowAdded(sender As Object, e As DataTableNewRowEventArgs)
            Updater()
        End Sub
        Private Sub Updater()
            RemoveHandler dt.TableNewRow, AddressOf RowAdded
            RemoveHandler dt.ColumnChanged, AddressOf ColumnChanged
            RemoveHandler dt.RowDeleted, AddressOf RowDeleted
    
            Dim result = dt.AsEnumerable().Select(Function(row) row.Field(Of Integer)("Price")).Sum()
    
            For Each row As DataRow In dt.Rows
                row.SetField(Of Integer)("Balance", result)
            Next
    
            AddHandler dt.TableNewRow, AddressOf RowAdded
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
            AddHandler dt.RowDeleted, AddressOf RowDeleted
        End Sub
    
        Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
            If e.Exception.Message = "Input string was not in a correct format." Then
                MessageBox.Show($"Invalid input for column {DataGridView1.Columns(e.ColumnIndex).Name}, correct or press ESC.")
            End If
        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

    • Marked as answer by no[one] Saturday, November 17, 2018 1:55 AM
    Saturday, November 17, 2018 1:38 AM
    Moderator
  • thank you it works perfectly but what if after updating the balance or clicking a button the DGV will be look like this

    3              12               0

    4              12               0

    5              12               0

    can you please help me i don't know what to do

    Saturday, November 17, 2018 4:10 AM
  • Here you go

    Public Class Form1
        Private dt As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Price", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Paid", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Balance", .DataType = GetType(Integer)})
            DataGridView1.DataSource = dt
    
            AddHandler dt.TableNewRow, AddressOf RowAdded
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
            AddHandler dt.RowDeleted, AddressOf RowDeleted
        End Sub
        Private Sub RowDeleted(sender As Object, e As DataRowChangeEventArgs)
            Updater()
        End Sub
        Private Sub ColumnChanged(sender As Object, e As DataColumnChangeEventArgs)
            Updater()
        End Sub
        Private Sub RowAdded(sender As Object, e As DataTableNewRowEventArgs)
            Updater()
        End Sub
        Private Sub Updater()
            RemoveHandler dt.TableNewRow, AddressOf RowAdded
            RemoveHandler dt.ColumnChanged, AddressOf ColumnChanged
            RemoveHandler dt.RowDeleted, AddressOf RowDeleted
    
            Dim result = dt.AsEnumerable().Select(Function(row) row.Field(Of Integer)("Price")).Sum()
    
            For Each row As DataRow In dt.Rows
                row.SetField(Of Integer)("Balance", result)
            Next
    
            AddHandler dt.TableNewRow, AddressOf RowAdded
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
            AddHandler dt.RowDeleted, AddressOf RowDeleted
        End Sub
    
        Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
            If e.Exception.Message = "Input string was not in a correct format." Then
                MessageBox.Show($"Invalid input for column {DataGridView1.Columns(e.ColumnIndex).Name}, correct or press ESC.")
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RemoveHandler dt.ColumnChanged, AddressOf ColumnChanged
            If dt.Rows.Count > 0 Then
                Dim balance = dt.Rows(0).Field(Of Integer)("Balance")
                For index As Integer = 0 To dt.Rows.Count - 1
                    dt.Rows(index).SetField(Of Integer)("Paid", balance)
                    dt.Rows(index).SetField(Of Integer)("Balance", 0)
                Next
            End If
            AddHandler dt.ColumnChanged, AddressOf ColumnChanged
        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

    • Marked as answer by no[one] Saturday, November 17, 2018 12:26 PM
    Saturday, November 17, 2018 10:32 AM
    Moderator