Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Calculate column in DataGridView
 

Calculate column in DataGridView

InVB.Net, how to create a DataGridView calculate column like this :

Date Description Debit Credit Balance

01/05/07 Income 500 500

01/06/07 Expenses 200300

01/07/07 Expenses 100 200

01/08/07 Income 200400

01/09/07 Expenses 100300

Can help me ? Thank you.........

Sixon  Friday, November 09, 2007 4:43 PM

Hiļ¼?Please check following code

Code Block

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("income", GetType(System.Int32))
dt.Columns.Add("income1", GetType(System.Int32))
dt.Columns.Add("sum", GetType(System.Int32))
dt.Rows.Add(500, 200)
dt.Rows.Add(0, 100)
dt.Rows.Add(200, 100)
Me.DataGridView1.DataSource = dt
End Sub
Private Sub DataGridView1_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValidated

Dim sum As Int32 = 0
For Each r As DataGridViewRow In Me.DataGridView1.Rows
sum = sum + r.Cells(0).Value - r.Cells(1).Value
r.Cells(2).Value = sum
Next
End Sub
End Class

If you have any further questions,feel free to let me know.

Best Regards

Gavin

Gavin Jin - MSFT  Tuesday, November 13, 2007 4:06 AM

Thank you very much.....it's really help me...

Sixon  Saturday, November 17, 2007 3:48 PM

Hiļ¼?Please check following code

Code Block

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("income", GetType(System.Int32))
dt.Columns.Add("income1", GetType(System.Int32))
dt.Columns.Add("sum", GetType(System.Int32))
dt.Rows.Add(500, 200)
dt.Rows.Add(0, 100)
dt.Rows.Add(200, 100)
Me.DataGridView1.DataSource = dt
End Sub
Private Sub DataGridView1_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValidated

Dim sum As Int32 = 0
For Each r As DataGridViewRow In Me.DataGridView1.Rows
sum = sum + r.Cells(0).Value - r.Cells(1).Value
r.Cells(2).Value = sum
Next
End Sub
End Class

If you have any further questions,feel free to let me know.

Best Regards

Gavin

Gavin Jin - MSFT  Tuesday, November 13, 2007 4:06 AM

Thank you very much.....it's really help me...

Sixon  Saturday, November 17, 2007 3:48 PM

You can use google to search for other answers

Custom Search

More Threads

• c# : How can i add these numbers on this area?
• Error occur when Add column after create bindingSource.
• DataGridView drag-n-drop between applications
• Separating TableAdapters from the Dataset, data source now missing
• .xsd datatable or dataset viewer missing
• What are the codes behind the Add New and Delete button of a BindingNavigator?
• Convert Excel file to MsAccess problem
• Datagridview onMouseClick onMouseDoubleClick
• What events change DataGridView.IsCurrentRowDirty?
• Bind Object Member to DataGridView Column