none
how to add datagridview column RRS feed

  • Question

  • Dear all 

    i have a question that how can i add specific column in datagridview and get the result in same datagridview 

    example

    i have 4 columns 

    account                 amount dr                    amount cr                  type

    cash                        10                                                                  dr

    bills                        20                                                                                                     dr

    salaries                   50                                                                                                       dr

    wages                       10                                                                                                  dr

    cash                                                       90                                     dr

    so here see the bold entry "cash" is the total of DR entries . this is what i need it keep total the DR entries 


    Saturday, July 6, 2019 7:36 AM

Answers

  • i did it see 

    this is a code . i place one textbox that is show actual value after SUM and then i add it in datagridview and done

     Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
    
    
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
    
    
                DataGridView1.Rows.Add(TextBox1.Text, Nothing, TextBox3.Text, TextBox4.Text)
                Dim tot As Double = 0.0#
    
    
                For i As Integer = 0 To DataGridView1.RowCount - 1
                    tot += Val(DataGridView1.Rows(i).Cells(2).Value)
    
    
                Next
    
                TextBox5.Text = tot
    
    
    
            
    
    
                        Dim io As Integer
                        io = DataGridView1.CurrentRow.Index
                        'txtName = datagridName.Item(columnName or columnNumber, rowNumber).value.tostring
    
                DataGridView1.Item(1, io).Value = TextBox5.Text
    
    
    
                    End If
          
    
         
    
    
        End Sub

    • Marked as answer by Omi4u Saturday, July 6, 2019 9:12 AM
    Saturday, July 6, 2019 9:12 AM

All replies

  • Hi

    Again, you are being difficult. You want a new Column to show totals? It makes more sense to show the totals in TextBoxes at bottom.

    Here is one way. This example uses a DataTable.Compute method. It keeps totals for DR and CR. I added Test data to show CR column Sum also.

    ' Form1 with DataGridView1
    ' and TextBox1 and TextBox2
    ' Labels to suit.
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With dt
    			.Columns.Add("Account")
    			.Columns.Add("Ammount.DR", GetType(Decimal))
    			.Columns.Add("Ammount.CR", GetType(Decimal))
    			.Columns.Add("Type")
    
    			.Rows.Add("Cash", 10, Nothing, "dr")
    			.Rows.Add("Test", Nothing, 10, "cr")
    			.Rows.Add("Bills", 20, Nothing, "dr")
    			.Rows.Add("Salaries", 50, Nothing, "dr")
    			.Rows.Add("Test2", Nothing, 20, "cr")
    			.Rows.Add("Wages", 10, Nothing, "dr")
    		End With
    		DataGridView1.DataSource = dt
    	End Sub
    	Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
    
    		TextBox1.Text = dt.Compute("SUM([Ammount.DR])", String.Empty).ToString
    		TextBox2.Text = dt.Compute("SUM([Ammount.CR])", String.Empty).ToString
    	End Sub
    End Class


    Regards Les, Livingston, Scotland




    • Edited by leshay Saturday, July 6, 2019 8:58 AM
    Saturday, July 6, 2019 8:55 AM
  • well i know how to calculate data in textbox but i was thinking to do it in datagrid. im doing it and i am very near to solve it ... let me do it then i will share it with you .
    Saturday, July 6, 2019 9:02 AM
  • i did it see 

    this is a code . i place one textbox that is show actual value after SUM and then i add it in datagridview and done

     Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
    
    
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
    
    
                DataGridView1.Rows.Add(TextBox1.Text, Nothing, TextBox3.Text, TextBox4.Text)
                Dim tot As Double = 0.0#
    
    
                For i As Integer = 0 To DataGridView1.RowCount - 1
                    tot += Val(DataGridView1.Rows(i).Cells(2).Value)
    
    
                Next
    
                TextBox5.Text = tot
    
    
    
            
    
    
                        Dim io As Integer
                        io = DataGridView1.CurrentRow.Index
                        'txtName = datagridName.Item(columnName or columnNumber, rowNumber).value.tostring
    
                DataGridView1.Item(1, io).Value = TextBox5.Text
    
    
    
                    End If
          
    
         
    
    
        End Sub

    • Marked as answer by Omi4u Saturday, July 6, 2019 9:12 AM
    Saturday, July 6, 2019 9:12 AM
  • Hi

    OK so you say you have discovered the answer. What happens if the User enters thr TYPE string into the Ammount DR column? What happens if the User enters a valid number into thr Ammount DR column?

    At least my offering calculasted the SUMs automatically as the cell valurs changed.


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, July 6, 2019 9:36 AM
    Saturday, July 6, 2019 9:35 AM