Mario, for a Running Balance you have to make a Template column and work on values in RowDatabound, also you should create a variable to hold last balance value between RowDatabound calls.
For a gridview like
#
Description
Credit
Debit
Balance
1
Add initial value
10
0
10
2
Energy
0
5
5
3
water
0
2
3
On aspx file add a button and gridview like bellow
' Class to hold sample data Public Class clsData
Public Property Description As String
Public Property Credit As Decimal
Public Property Debit As Decimal
Public Sub New()
End Sub
End Class
Protected Sub zLoadData()
' Create some sample data
Dim sample1 As New clsData With {.Description = "Add initial value", .Credit = 10, .Debit = 0}
Dim sample2 As New clsData With {.Description = "Energy", .Credit = 0, .Debit = 5}
Dim sample3 As New clsData With {.Description = "Water", .Credit = 0, .Debit = 2}
' Create a list and samples to list
Dim lstData As New List(Of clsData)
lstData.Add(sample1)
lstData.Add(sample2)
lstData.Add(sample3)
' Set datasource & bind
GridView1.DataSource = lstData
GridView1.DataBind()
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
zLoadData() ' Load some sample on gridview
End Sub
Private zBalance As Decimal = 0 ' Set to your starting Balance value
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim zCredit As Decimal = e.Row.Cells(2).Text
Dim zDebit As Decimal = e.Row.Cells(3).Text
Dim lblBalance As Label = e.Row.Cells(4).FindControl("lblBalance")
zBalance = zBalance + zCredito - zDebito
lblBalance.Text = zBalance
End If
End Sub
Member
396 Points
919 Posts
gridview as Bank Extract
Oct 30, 2018 10:13 AM|mariolopes|LINK
Hi
I Need to show one gridview as a Bank extract like the following (saldo strats as 0)
id | opera | valor | saldo |
1 | asas|10|10
2|sdsd|5|15
3|sdsadas|-6|9
Hw can I get the column saldo as result from column valor?
Thank you
https://programamos.pt
Member
466 Points
351 Posts
Re: gridview as Bank Extract
Oct 30, 2018 11:10 PM|jzero|LINK
Mario, for a Running Balance you have to make a Template column and work on values in RowDatabound, also you should create a variable to hold last balance value between RowDatabound calls.
For a gridview like
On aspx file add a button and gridview like bellow
On code behind
Participant
850 Points
304 Posts
Re: gridview as Bank Extract
Oct 31, 2018 04:19 AM|Jenifer Jiang|LINK
Hi mariolopes,
According to your description, I suggest that the key point of your requirement is to calculating the saldo in the event RowDataBound.
You could set a variable to save the saldo you've calculated as the session I've set in the following code.
.Aspx
Code-behind.
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("id"), new DataColumn("opera"), new DataColumn("valor")}); dt.Rows.Add("1", "asas", "10"); dt.Rows.Add("2", "sdsd", "5"); dt.Rows.Add("3", "sdsadas", "-6"); GridView1.DataSource = dt; GridView1.DataBind(); } } int saldovalue = 0; protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label valor = e.Row.FindControl("lblvalor") as Label; Label saldo = e.Row.FindControl("lblsaldo") as Label; int session = saldovalue; saldovalue = session + Convert.ToInt32(valor.Text); saldo.Text = saldovalue.ToString(); } }
result:
Best Regards,
Jenifer