locked
gridview as Bank Extract RRS feed

  • Question

  • User-909867351 posted

    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

    Tuesday, October 30, 2018 10:13 AM

Answers

  • User-943250815 posted

    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

    <asp:Button ID="Button1" runat="server" Text="Button" />
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
          <asp:TemplateField HeaderText="#">
            <ItemTemplate><%# Container.DataItemIndex + 1 %></ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" />
          <asp:BoundField DataField="Credit" HeaderText="Credit" ReadOnly="True" />
          <asp:BoundField DataField="Debit" HeaderText="Debit" ReadOnly="True"  />
          <asp:TemplateField HeaderText="Balance">
            <ItemTemplate>
              <asp:Label ID="lblBalance" runat="server" Text=""></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>

    On code behind

    ' 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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 30, 2018 11:10 PM
  • User839733648 posted

    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

                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
                    <RowStyle HorizontalAlign="Center" />
                    <Columns>
                        <asp:BoundField DataField="id" HeaderText="id" ItemStyle-Width="150">
                            <ItemStyle Width="150px"></ItemStyle>
                        </asp:BoundField>
                        <asp:BoundField DataField="opera" HeaderText="opera" ItemStyle-Width="150">
                            <ItemStyle Width="150px"></ItemStyle>
                        </asp:BoundField>
                        <asp:TemplateField HeaderText="valor">
                            <ItemTemplate>
                                <asp:Label ID="lblvalor" runat="server" Text='<%# Eval("valor") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="saldo">
                            <ItemTemplate>
                                <asp:Label ID="lblsaldo" runat="server"></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 31, 2018 4:19 AM

All replies

  • User-943250815 posted

    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

    <asp:Button ID="Button1" runat="server" Text="Button" />
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
          <asp:TemplateField HeaderText="#">
            <ItemTemplate><%# Container.DataItemIndex + 1 %></ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" />
          <asp:BoundField DataField="Credit" HeaderText="Credit" ReadOnly="True" />
          <asp:BoundField DataField="Debit" HeaderText="Debit" ReadOnly="True"  />
          <asp:TemplateField HeaderText="Balance">
            <ItemTemplate>
              <asp:Label ID="lblBalance" runat="server" Text=""></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>

    On code behind

    ' 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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 30, 2018 11:10 PM
  • User839733648 posted

    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

                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
                    <RowStyle HorizontalAlign="Center" />
                    <Columns>
                        <asp:BoundField DataField="id" HeaderText="id" ItemStyle-Width="150">
                            <ItemStyle Width="150px"></ItemStyle>
                        </asp:BoundField>
                        <asp:BoundField DataField="opera" HeaderText="opera" ItemStyle-Width="150">
                            <ItemStyle Width="150px"></ItemStyle>
                        </asp:BoundField>
                        <asp:TemplateField HeaderText="valor">
                            <ItemTemplate>
                                <asp:Label ID="lblvalor" runat="server" Text='<%# Eval("valor") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="saldo">
                            <ItemTemplate>
                                <asp:Label ID="lblsaldo" runat="server"></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 31, 2018 4:19 AM