locked
Show Total In Foruth Column Of Gridview RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my Gridview Code

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                    DataSourceID="SqlDataSource1">
                    <Columns>
                        <asp:CommandField ShowEditButton="True" />
                        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                            SortExpression="ID" />
                        <asp:BoundField DataField="Field1" HeaderText="Field1" SortExpression="Field1" />
                        <asp:TemplateField HeaderText="Field2" SortExpression="Field2">
                            <EditItemTemplate>
                                <asp:Label ID="ELabel1" runat="server" Text='<%# Bind("Field2") %>'></asp:Label>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Field2") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Field3" HeaderText="Field3" SortExpression="Field3" />
                        <asp:TemplateField HeaderText="Field4" SortExpression="Field4">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" onchange ="CheckNumber(this);" Text='<%# Bind("Field4") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Field4") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    My Output look like this below

    I want out on Field5 like below (Field2 + Field3 - Field4)

    Field5

    55

    55

    45

    100

    Friday, November 4, 2016 4:59 PM

Answers

  • User-691209617 posted

    Hi,

    Add row data bound event on grid and following code in rowbonud data event.

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
                   {
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                             int Field2 = Convert.ToInt32(e.Row.Cells[3].Text);
                             int Field3 = Convert.ToInt32(e.Row.Cells[4].Text);
                             int Field4 = Convert.ToInt32(e.Row.Cells[5].Text);
                             e.Row.Cells[6].Text = (Field2 + Field3-Field4).ToString();
       
                        }
                   } 

    Hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 4, 2016 7:53 PM

All replies

  • User475983607 posted

    Just do the total in your SQL query. 

    http://stackoverflow.com/questions/14877797/how-to-sum-two-fields-within-an-sql-query

    Friday, November 4, 2016 5:05 PM
  • User2033107836 posted

    I want to do in asp.net page and C#

    Friday, November 4, 2016 6:58 PM
  • User-691209617 posted

    Hi,

    Add row data bound event on grid and following code in rowbonud data event.

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
                   {
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                             int Field2 = Convert.ToInt32(e.Row.Cells[3].Text);
                             int Field3 = Convert.ToInt32(e.Row.Cells[4].Text);
                             int Field4 = Convert.ToInt32(e.Row.Cells[5].Text);
                             e.Row.Cells[6].Text = (Field2 + Field3-Field4).ToString();
       
                        }
                   } 

    Hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 4, 2016 7:53 PM