Solved

Multiplying Columns in a DataGrid

Posted on 2004-10-04
33
503 Views
Last Modified: 2008-03-17
Please help!  What I want to do is SO simple, but getting information on anything other than a simple DataGrid seems impossible.

I am setting up a datagrid for a shopping cart, with the item and its amount populated from the database, no problem.  I want to add two template columns, one with a textbox control, defaulted to 1, for the quantity, and another with a label that displays the amount x qty.  The next step will be to add an update button so that if the user changes the quantity, they can update the total amount.  I just cant seem to work out how to display the total for each line when the grid loads - any clues?

If anyone knows of any good resources that go beyond standard data controls and explains how to programmatically control them, please let me know!!

Thanks
0
Comment
Question by:BlueEdger
  • 14
  • 11
  • 6
  • +1
33 Comments
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
PS - Im coding in VB.NET
0
 
LVL 15

Assisted Solution

by:praneetha
praneetha earned 50 total points
Comment Utility
you can write cod ein itemdatabound

and access what is in a datagrid row using e.Item.Cells[0].Text-would give first column..

TextBox txt=(TextBox)e.Item.cells[0].FindControl("textboxname");//sorry this is C#...
txt.Text- contains the updated quantity...

0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
I have a function that loads the datagrid with no arguments.  Therefore e is not available to me in this function, so should I do the multiplication in a new function?
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 75 total points
Comment Utility
I would add a template column and make an itemdatabound subroutine to handle the OnItemDataBound event (put OnItemDataBound="DataGrid1_ItemDataBound" in the HTML source between the datagrid tags

Each time a row is added, it will call this subroutine and you can do math with each cell of the row(item)

    Protected Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
         'assuming template column will be column #7 and the dropdown list is column 5 and the quantity is column 6
         e.item.cells(7).text = CStr(CDbl(e.item.cells(6).text) * CDbl(CType(e.item.cells(5).controls(0) ,DropDownList).SelectedItem.Text)
    End Sub

Aeros
0
 
LVL 15

Expert Comment

by:praneetha
Comment Utility
r u adding datagrid dynamically....

item databound is executed adter datagrid is bound....

0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
AerosSaga - I've tried the following:

    Protected Sub dgTest2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest2.ItemDataBound
        e.Item.Cells(1).Text = CStr(CDbl(e.Item.Cells(0).Text) * CDbl(e.Item.Cells(3).Text))
    End Sub

Had to remove the dropdown ref, as I havent got one, qty is in cell 0 and amount is in 3, amount is supplied by the db, is that why I am getting the following error:

Input string was not in a correct format
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
what happens if you try this:

 e.Item.Cells(1).Text = CDbl(e.Item.Cells(0).Text) * CDbl(e.Item.Cells(3).Text))
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
No, same error
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
check the quick watch for those two expressions and see what values you are getting
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
I think the problem is with the quantity.  I've just put make cell 1 the text of cell 0 or 3, and it does it for amount but not for quantity.  It also changes the title of cell 1 to the other column?
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
You may need to create intermediary variables in double like

Dim Mydbl2 As Double = CDbl(e.Item.Cells(0).Text)
Dim Mydbl2 As Double = CDbl(e.Item.Cells(3).Text)

then multiply your two variables and write it back out as a string to your datagrid cell
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
check your ordinals then, are there invisible columns?
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
Can I stop it trying to do the calculation on the header row?
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
why are you trying to do it in the header anyway?
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
It just wont recognise the contents of the textbox in Cells(0).  I've put an if statement in so it only does the calculation if cells(0).text <> qty (the title of the column)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
If you want the in the header/footer check here:
http://aspnet.4guysfromrolla.com/articles/020503-1.aspx

Aeros
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
Im not trying to do it in the header!!!!!  So why is it trying to calculate the header?  Here is my datagrid:

<asp:DataGrid id="dgTest2" runat="server" Font-Size="10px" Font-Names="Arial" CellPadding="2" OnItemDataBound="dgTest2_ItemDataBound">
<HeaderStyle Font-Size="11px" Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Qty">
<ItemTemplate>
      <asp:TextBox id="qty" runat="server" Columns="10">2</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
      <asp:TemplateColumn HeaderText="SubTotal">
      <ItemTemplate>
      <asp:Label Runat="server" ID="subtotal"></asp:Label>
      </ItemTemplate>
      </asp:TemplateColumn>
</Columns>

The other columns are greated dynamically.  Is that the problem?  Because I have a mixture of columns?
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Yes it is
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
are you recreating said dynamic columns on postback?
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
Havent got as far as postback, at the moment just have binddata function and then this itemdatabound function on page load.  
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
Im going to change it to bound columns
0
 
LVL 15

Expert Comment

by:praneetha
Comment Utility
  Protected Sub dgTest2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest2.ItemDataBound

if  e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer// do this

        e.Item.Cells(1).Text = CStr(CDbl(e.Item.Cells(0).Text) * CDbl(e.Item.Cells(3).Text))
    End Sub
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
I just cant get the value of the textbox.  Is it not e.Item.cells(0).Text if its in a textbox?
0
 
LVL 15

Expert Comment

by:praneetha
Comment Utility
TextBOx textbox1=(TextBOx)e.Item.cells(0).FindControl("Textboxname");

try that...and access...using textbox1.text
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
In VB?
0
 
LVL 15

Expert Comment

by:praneetha
Comment Utility
just however you would typecast in VB...
0
 
LVL 15

Expert Comment

by:praneetha
Comment Utility
Dim I As TextBox = DirectCast(e.Item.cells(0).FindControl("Textboxname"), TextBox)

may be like that

or

Dim I As TextBox = CType(e.Item.cells(0).FindControl("Textboxname"), TextBox)
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
Have to go now, thanks for your help guys, will continue with this tomorrow
0
 
LVL 15

Expert Comment

by:Thogek
Comment Utility
Sounds like praneetha has the idea.  To attempt to translate his suggestion to VB:

    If Not e.Item.ItemType = ListItemType.Header  And  Not e.Item.ItemType = ListItemType.Footer Then
        ' current Item is not a header or footer row
        e.Item.Cells(1).Text = CStr( CDbl(e.Item.Cells(0).Text) * CDbl(e.Item.Cells(3).Text) )
    End If

or, more directly

    If e.Item.ItemType = ListItemType.Item  Or  e.Item.ItemType = ListItemType.AlternatingItem Then
        ' current Item is an Item or AlternatingItem
        e.Item.Cells(1).Text = CStr( CDbl(e.Item.Cells(0).Text) * CDbl(e.Item.Cells(3).Text) )
    End If
0
 
LVL 1

Author Comment

by:BlueEdger
Comment Utility
OK, looking at this fresh this morning, I seem to have got there at last!!

This seems to have worked:

    Protected Sub dgTest2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest2.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            ' current Item is an Item or AlternatingItem
            Dim l As Label = CType(e.Item.Cells(1).Controls(1), Label)
            l.Text = CStr(Integer.Parse((CType(e.Item.Cells(0).Controls(1), TextBox)).Text) * CDbl(e.Item.Cells(3).Text))
        End If
    End Sub

This proved a good source on the subject:

http://www.dotnetjunkies.com/Tutorial/FF3A910D-17C0-464D-AB47-89A98C5D1355.dcik

I need to refine it for formatting etc, but the base functionality works.  Phew!  Is all of .NET this hard??  Im pining for Classic ASP!!!

All of the books I have on .NET only touch on standard data controls, and dont go into the nitty gritty of programmatically setting them.  If anyone knows of a good reference book that will help me with this, please let me know - Im desperate for help!!!

Thanks for all your help guys - I'll leave the question open a bit longer in case there is anything to add to this subject, then award points.  
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Its not always this hard, you just had very specific requirements for a prefab control.  Once you get the hang of it, its imeasurably easier, and less time consuming than Classic ASP

Aeros
0
 
LVL 15

Expert Comment

by:Thogek
Comment Utility
> Is all of .NET this hard??  

ASP.NET involves a lot more structure (and thus more stuff to learn) than "classic" ASP, but once you get oriented within all that structure, some things do indeed become easier, and you may find yourself with easier ways to do much more powerful things....

Of course, that's all subjective.  (YMMV, and all that. :-)

> If anyone knows of a good reference book

There are lots of ASP.NET books out there.  Personally, I don't like most of the ones I've seen, 'cause most of 'em gloss over the details of the ASP.NET-to-HTML relationship and many other details which I consider rather important.

My favorite so far has been http://www.amazon.com/exec/obidos/ASIN/0789728222/tgk-20 (whether interested in pursuing the MCAD/MCSD or not) -- but again, this is *very* subjective.  If you're interested in a good book, I'd suggest camping out in your local bookstore(s) and going through as many candidates as you can find until you find one that *you* like.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now