add 2 columns together in a datagrid?

How can I add 2 columns in one row together in a datagrid and display in another row the sum?  I do not want to sum a column and display it in the footer.  I cant do this in my Select statement it has to be done in the datagrid.

thanx
weisenbr
0
weisenbr
1/9/2004 4:08:47 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
647 Views

Similar Articles

[PageSpeed] 22
Get it on Google Play
Get it on Apple App Store

<asp:Boundcolumn DataField="Field1" />

<asp:BoundColumn Datafield="field2" />
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label id="Sum" runat="server" Text='<%#(Cint(DataBinder.Eval(Container.DataItem,"Field1")) + Cint(DataBinder.Eval(Container.DataItem,"Field2"))).ToString()%>' />
</ItemTemplate>
</asp:TemplateColumn>
This is likely to have a bug or two, but you get the idea. Alternately you could use the ItemDataBound Event.
Darren
0
daccardo
1/9/2004 5:30:29 PM
Does the below given code help

Its table Products from Northwind DataBase

<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" style="Z-INDEX: 101; LEFT: 42px; POSITION: absolute; TOP: 145px" runat="server">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Databinder.Eval(Container.DataItem , "ProductId") %>
<%#Databinder.Eval(Container.DataItem , "ProductName") %>
</td>
</tr>
<tr>
<td><%#Databinder.Eval(Container.DataItem , "Total", "{0:c}") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
cn = New SqlConnection("Server=localhost;uid=sa;pwd=;database=northwind;")
da = New SqlDataAdapter("Select * from Products", cn)
ds = New DataSet()
da.Fill(ds, "Table")
Dim dc As DataColumn
dc = New DataColumn("Total", Type.GetType("System.Double"))
dc.Expression = "UnitPrice * UnitsInStock"
ds.Tables(0).Columns.Add(dc)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End If
End Sub

Sushila Bowalekar Patel
Visual ASP/ASP.NET MVP
http://weblogs.asp.net/sushilasb
0
SushilaSB
1/9/2004 5:33:07 PM
the above dont help everything that I read just teels me how to add a total at the bottom I need these to ad horizontaly.  Im trying this in the OnItemDatabound with code below but it doesnt return anything in column 20?

If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Cells(20).Text = e.Item.Cells(18).Text + e.Item.Cells(19).Text
End If
0
weisenbr
1/9/2004 7:55:59 PM
If e.Item.ItemType = ListItemType.Item Or _ 

e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Cells(20).Text = (Int.Parse(e.Item.Cells(18).TextM) + Int.Parse(e.Item.Cells(19).Text)).ToString()
End If

Darren
0
daccardo
1/9/2004 10:42:11 PM
Reply: