Click here to Skip to main content
15,036,145 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a form application. In this application I used a datagridview. There are 4 columns. I want to add all numeric values of all 4th columan's cell. How Can I do this?
Posted
Comments
[no name] 27-Aug-13 10:25am
   
In C# you add numeric values using the + (addition) operator.
Sergey Alexandrovich Kryukov 27-Aug-13 11:24am
   
:-)!!!
—SA
Sergey Alexandrovich Kryukov 27-Aug-13 11:26am
   
This is directly allowed by this control's design. How could it be a problem? http://whathaveyoutried.com?
—SA
ridoy 27-Aug-13 13:42pm
   
that make no sense at all.

1 solution

How about it?

Default.aspx
============
XML
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false" >
            <Columns>
                <asp:BoundField DataField="Val1" HeaderText="Column1" />
                <asp:BoundField DataField="Val2" HeaderText="Column2" />
                <asp:BoundField DataField="Val3" HeaderText="Column3" />
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                        <%# int.Parse(Eval("Val1").ToString()) + int.Parse(Eval("Val2").ToString()) + int.Parse(Eval("Val3").ToString())%>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
</asp:Content>


Default.aspx.cs
===============

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SignERP
{
public partial class Testing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
gvTest.DataSource = GetData();
gvTest.DataBind();
}

protected static List<numberrow> GetData()
{
List<numberrow> valueList = new List<numberrow>();
NumberRow row1 = new NumberRow();
row1.Val1 = 1;
row1.Val2 = 1;
row1.Val3 = 1;

NumberRow row2 = new NumberRow();
row2.Val1 = 2;
row2.Val2 = 2;
row2.Val3 = 2;

valueList.Add(row1);
valueList.Add(row2);

return valueList;
}

public class NumberRow
{
public int Val1 { get; set; }
public int Val2 { get; set; }
public int Val3 { get; set; }

public NumberRow()
{
Val1 = 0;
Val2 = 0;
Val3 = 0;
}
}
}
}

Thanks and Best Regards
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900