Simple Math in Gridview

P

Phillip Vong

Newbie here trying to learn.

Using VS2005 / learning in VB.NET for an ASPX page.

Sorry for this simple question. I have a Gridview1 with 2 columns. I added
a 3rd column and all I want to do is add the numbers in the 1st and 2nd
column together for the 3rd column. Can someone tell me where I can put the
code in Visual Studio 2005 and how I tell the Gridview to do the simple
math?

Is there a site that will teach me exactly this? I googled but didn't find
exactly this.
 
G

Guest

Hi,
Here is a simple way to do what you are looking for
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("FirstColumn",typeof(int));
dt.Columns.Add("SecondColumn", typeof(int));
DataRow dr = dt.NewRow();
dr["FirstColumn"]=34;
dr["SecondColumn"]=12;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["FirstColumn"]=7;
dr["SecondColumn"]=4;
dt.Rows.Add(dr);

grd.DataSource=dt;
grd.DataBind();
}
}

</script>
<asp:GridView runat="server" ID="grd" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FirstColumn" HeaderText="First" />
<asp:BoundField DataField="SecondColumn" HeaderText="Second"
/>
<asp:TemplateField HeaderText="Sum">
<ItemTemplate>
<%# ((int)Eval("FirstColumn"))
+((int)Eval("SecondColumn")) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Date math 3
Delete rows in Gridview 0
How to set Gridview colum size ? 1
GridView 3
gridview 0
sorting gridview question 6
GridView Question 1
GridView 0

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top