Need to display total of a column in a datagrid

Hi there, 

I have situation to display the total/sum of a coumn in a datagrid. and i try doing it using Itemdatabound event. Other than this total i have some thing to display before i bind the to the grid. I am fine with the rest of the stuff but when i code like below
Dim variance As Integer = Convert.ToInt16(DataBinder.Eval(e.Item.DataItem, "Variance"))
TotalVariance += Convert.ToInt16(DataBinder.Eval(e.Item.DataItem, "Variance")) in the itemdatabound it says input string was not in a correct format. I am not sure if i am doing some thing wrong.
Below is the whole itemdatabound event code i have which includes few other things too.

Private Sub grdPartsList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdPartsList.ItemDataBound
'Dim l_vntstrText As String
'Dim l_vnttd As TableCell
'Dim l_vntdueDate As Date
Dim btn As Button

If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
'get Controlled_Matl yesNo
If CType(DataBinder.Eval(e.Item.DataItem, "Controlled_Matl"), String) = "0" Then
CType(e.Item.FindControl("lblCtrlM"), TASLabel).Text = "N"
Else
CType(e.Item.FindControl("lblCtrlM"), TASLabel).Text = "Y"
End If
'
If CType(DataBinder.Eval(e.Item.DataItem, "Material_Change"), String) = "0" Then
CType(e.Item.FindControl("lblMatlChg"), TASLabel).Text = "N"
Else
CType(e.Item.FindControl("lblMatlChg"), TASLabel).Text = "Y"
End If
If CType(DataBinder.Eval(e.Item.DataItem, "Buy_Part"), String) = "0" Then
CType(e.Item.FindControl("lblButPart"), TASLabel).Text = "B"
Else
CType(e.Item.FindControl("lblButPart"), TASLabel).Text = "BIA"
End If
If CType(DataBinder.Eval(e.Item.DataItem, "Color_Part"), String) = "0" Then
CType(e.Item.FindControl("lblColorPart"), TASLabel).Text = "N"
Else
CType(e.Item.FindControl("lblColorPart"), TASLabel).Text = "Y"
End If
If CType(DataBinder.Eval(e.Item.DataItem, "New_Tool"), String) = "0" Then
CType(e.Item.FindControl("lblNewTool"), TASLabel).Text = "N"
Else
CType(e.Item.FindControl("lblNewTool"), TASLabel).Text = "Y"
End If
If CType(DataBinder.Eval(e.Item.DataItem, "Estimate"), String) = "0" Then
CType(e.Item.FindControl("lblEstimate"), TASLabel).Text = "N"
Else
CType(e.Item.FindControl("lblEstimate"), TASLabel).Text = "Y"
End If

****start error part*******
****here is where it says input string was not in a correct format.*******
Dim variance As Integer = Convert.ToInt16(DataBinder.Eval(e.Item.DataItem, "Variance"))
TotalVariance += Convert.ToInt16(DataBinder.Eval(e.Item.DataItem, "Variance"))
'TotalVariance += Convert.ToInt32(e.Item.Cells(16).Text)
'TotalVariance += variance
******** End error part*****
Dim r As Label
r = e.Item.FindControl("lblgridFindUsage")
r.Text = "<input type=radio name='myradiogroup' value=" & e.Item.Cells(28).Text & ">"
'If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
' btn = CType(e.Item.Cells(0).FindControl("btnDelete"), Button)
' btn.Attributes.Add("onclick", "return confirm_delete();")
'End If
'e.Item.fo
ElseIf e.Item.ItemType = ListItemType.Footer Then
If e.Item.Cells(10).Text.Equals("Old Stats") Then
e.Item.Cells(10).ColumnSpan = 3
e.Item.Cells(10).Attributes.Add("align", "center")
e.Item.Cells.RemoveAt(12)
e.Item.Cells.RemoveAt(11)

End If
If e.Item.Cells(11).Text.Equals("New Stats") Then
e.Item.Cells(11).ColumnSpan = 3
e.Item.Cells(11).Attributes.Add("align", "center")
e.Item.Cells(11).BackColor = System.Drawing.Color.LightBlue
'e.Item.Cells(11).BackColor = Color.FromArgb(237, 249, 247)
'e.Item.BackColor = Color.FromArgb(204, 204, 255)
e.Item.Cells.RemoveAt(14)
e.Item.Cells.RemoveAt(13)
e.Item.Cells.RemoveAt(12)
e.Item.Cells(15).BackColor = Color.FromArgb(2, 237, 249, 247)
e.Item.Cells(14).BackColor = Color.FromArgb(2, 237, 249, 247)
e.Item.Cells(13).BackColor = Color.FromArgb(2, 237, 249, 247)
e.Item.Cells(12).BackColor = Color.FromArgb(2, 237, 249, 247)

End If
End If
End Sub

Please help
and thanks in advance.
-D
0
lusty_learner
7/8/2004 3:13:23 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
427 Views

Similar Articles

[PageSpeed] 15

How are you loading the Datagrid? 

You could make it a whole lot easier if loading from a database if you let the SQL do the math and then load that as a column.
For example:
Dim strSQL as string = "SELECT ColumnA, ColumnB, ColumnA + ColumnB as ColumnC FROM MyTable"
Then bind the SQL to the Grid and the three columns will be in the grid.
You can then use the ItemDataBound Event to set whatever color, font, or other properties as needed on each row.

Other things can be done in the SQL as well to update the data before it even gets to the grid.
Example:
SQL SERVER
SELECT CASE ColumnA WHEN 1 THEN 'Y' ELSE 'N' END as YNField FROM MyTable
MSAccess
SELECT IIF(ColumnA = 1, 'Y', 'N') AS YNField FROM MyTable

And as for putting the events on the buttons in the ItemDataBound event, I prefer to put them in in the Procedure that loads the grid (keeps the code cleaner and easier to read)
For Example:

Private Sub LoadGrid
.... Code that loads the Datagrid
Dim itm as DataGridItem
For Each itm in DataGrid1.Items
' Replace x with the column that has the delete button (remember grids are 0 based)
' Replace "cmdDelete" with the name of your button
CType(itm.cells(x).FindControl("cmdDelete"), Button).Attributes.Add("onclick", "return _
(confirm('Are you sure you want to delete this item?'));")
Next
End Sub

I know this is probably more than you asked for but I teach this stuff on the side and I believe in making things easier on the programmer and the datagrid is my favorite control to teach.

Leo Mrozek
MCP, ASP, ASP.NET, VB, VB.NET, SQL Server
Round Lake, IL, USA
Leo Mrozek

Microsoft Certified Professional

ASP, ASP.NET, VB, VB.NET, SQL Server

Round Lake, IL, USA
0
LeoM1895
7/8/2004 3:47:40 PM
Hi Leo,

Summing a loumn in the stored proc is also a good idea. I am joining two 3 tables to bring the data to the screen so i just tried using
Dim variance As Integer = Convert.ToInt16(DataBinder.Eval(e.Item.DataItem, "Variance"))
But for some reason it doesn't do it.
As it doesn't work the only option i have is to do it in the backend. I am just curious why it doesn't work :)
Thanks for the help.
0
lusty_learner
7/8/2004 6:49:34 PM
Reply:

Similar Artilces:

How to display column total of numeric columns in the datagrid footer
I able to connect Datagrid control to ADO.Net for pulling data. But I can not able to display total of a numeric field column in the Datagrid footer. Is it possible? How? Please tell me any one. Hi, please read up Displaying Column Summary Information in the Footer and HOW TO: Create a Summary Row for a DataGrid in ASP.NETHope this helps..Alvin ChooiMicrosoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia Blog : http://alvinzc.blogspot.com...

Displaying image in a column of a datagrid control
Hi Experts, How can i display an image in a column of a datagrid control? On clicking that image i want to redirect to another screen based on the row selected. Also how should i open an popup screen by clicking the hyperlink column of a datagrid control. It is similar to a message board forum.(viewing all the messages posted with details) Thanks in advance. Hi, you can take a look at this article. There are many others around when you google for it. Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Workaround for non working Mark as answer buttons....

How do I total the column of a datagrid, in VB.net ?
  Hey Everybody... I have 1 column in my datagrid that needs to be added up to get the total.  The column is called "TotalMoney". I use VB.NET for code behind....   would someone give me an overview as to how I can do this ? I don't need the result in the footer or anything like that,  I just need to be able to show the value on a Label somewhere on the page.    **********************Newbie 4-Eva  Dim adpt As SqlDataAdapter        Dim dt As DataTable        Dim total As Int3...

Help Needed -Displaying Image in the Datagrid Column
How to display a image in the dat grid column. i am new to this asp.net. i am using c# for coding ASP.NET. Can any one help me. urgent. thanks use template column to insert an <image> inside ur datagrid, like this : <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:Image Runat=server ID="img1"></asp:Image> </ItemTemplate> </asp:TemplateColumn> </Columns> ...

I need to get the total from a one column in a Grid Controller
I need to get the total from a Grid Controller of 1 columns and several more for a datagrid that uses a store procedure is there a wayThank You in advance you can do it many ways.. here is one way .. where u can find the total of any field(numeric) on rowbound method of datagrid...<asp:GridView id="Mygrd" DataSourceID="Mysrc" OnRowDataBound="Mygrd_RowDataBound" AutoGenerateColumns="false" ShowFooter="true" Runat="server"> <Columns> <asp:BoundField DataField="Myfield" Heade...

DataGrid etc does not display when using VS.NET server controls
This thing is driving me nuts! I know you should be able to do it with code but I want to be able to drag a dataadapter onto a page, set up the dataset from that and bind it to a datagrid without writing a line of code. Everytime I do it whether I am using a remote SQL Server or a local MSDE for the datasouce all I get is a blank page. The source contains the form and the __VIEWSTATE and that is it. If I add in the pageload codebehind datagrid1.databind() then I get the column headings and that is all. Any ideas? Regards, DaveB You always have to call DataBind, that's for sure....

calculating column total for 2 different datagrids and displaying in 3rd
Hello- Is it possible to have two datagrids and then have a 3rd datagrid the displays each column totals for 1 and 2. All 3 tables have the same number of columns and only have 1 row in each. Any help would be appreciated. Thanks...

Totaling a column in the datagrid (column is a TimeSpan)
Here is the problem... I have one column in my datagrid that is the TimeSpan of two other columns... Cell 3 is an "in" time, cell 4 is an "out" time and cell 5 is the TimeSpan. I want to add a label control to the page that adds up all of the TimeSpan rows. Normally (if the column was a price or other int) I could use the OnItemDataBound and sum it up, but the format of the TimeSpan (hh:mm:ss) is throwing it all off... I have tried to just add them, but it just adds the strings together - not much help. I have also tried to use the SubString method and t...

Sum or Total of a column in DataGrid changes when I click on Edit Row
Hi,   I am summing a column in my DataGrid and displaying total at the footer with the below piece of code If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then 'Snip out the ViewCountDim ContributionWithHeld As Integer = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ContributionWithHeld")) myTotalContributionWithHeld += ContributionWithHeld ElseIf e.Item.ItemType = ListItemType.Footer Thene.Item.Cells(20).Text = "Total: " & String.Format("{0:#,###}", myTotalContributionWithHeld) End If and it...

Re: How to control the columns displayed on a .net page depending on the page the user is coming from
  Hi all,    I have got two pages on wy website which contains a form to submit data into the same database. The two pages have also got a link to view the submitted data. The challenge I have got at the moment is, I want to be able to control columns the users see depending on the which of the two pages they are coming from. I know I can simply create two more separate pages but I don't want to do that. I feel I can be clever and achieve this with a conditional statement but to be honest, I don't know how to go about this. Could you please help me on thsi? Bas...

How to display the sum of a column at the footor of the datagrid column
HI, Any idea how I can display the Sum of any column at the Footor of that Column. HighOnCodingWanna get high! Hi, Take a look @ Displaying Column Summary Information in the Footer HTH K B ShajudeenAreYaar.comDubai, United Arab Emirates....

display the sum of two datagrid columns into third column
hi... there..     i have a datagrid with columns named Price ,Quantity and total. now in the total column i  wanna display the prices which comes by multiplyiing the price and quantity (like  total=Price * quantity). I'm workin in asp.net +vb.net.                     may i knw hw can i do this.. plz write the code.. thanks.         Hi, do like this..suppose u have,        string ProductPrice = ...

column header to span two columns in a datagrid display
Is it possible for a column header to span two columns? If so how would this be done? Thanks In the ItemDataBound (or ItemCreated) check if Item is of type Header and if so - define the colspan property of the Cell object which you want to define the Colspan for and delete the next Cell from hte DataGridItem CheersBug [MCSD] Is it possible to span header column in design view only. using some kind of CSS. I'm using ASP,NET 3.5 and GridView...

Web resources about - Need to display total of a column in a datagrid - asp.net.presentation-controls

Want to preview our new DataGrid for Xamarin.Forms?
tl;dr Zumero.DataGrid is a Xamarin.Forms control for displaying data in rows and columns. If you would be interested in testing and previewing ...

Mad King DataGrid
This post is about scrolling in WPF and the egocentric DataGrid control. I’ll give the project background but if all you’re interested in is ...

Stack Overflow
Stack Exchange log in - careers - chat - meta - about - faq Questions Tags Users Badges Unanswered Ask Question Top Questions interesting 295 ...

Microsoft Silverlight - Wikipedia, the free encyclopedia
Microsoft Silverlight is an application framework for writing and running rich Internet applications , with features and purposes similar to ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Casaba Security » Products » X5s Testing Tool For Finding XSS Vulnerabilities
Security analysis, consulting, development and testing partners who understand your business strategy.

CodeGuru - Microsoft developers related ideas, articles, tips, tricks, comments, downloads, and so much ...
CodeGuru is where developers can come to share ideas, articles, questions, answers, tips, tricks, comments, downloads, and so much more related ...

Caliburn Micro: WPF, Silverlight, WP7 and WinRT/Metro made easy.
A small, yet powerful framework designed for WPF, Silverlight and WP7. It implements a variety of UI patterns for solving real-world problems. ...

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 11 Jul 2012

license-discuss@opensource.org: 11636: first draft of license proliferation committee report
FYI,the first draft report of the OSI's License Proliferation Committee.To join the OSI's license proliferation discuss email list, send anemail ...

Resources last updated: 11/29/2015 2:21:51 AM