Solved

Multiplying Columns in a DataGrid

Posted on 2004-10-04
33
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 11
  • 6
  • +1
33 Comments
 
LVL 1

Author Comment

by:BlueEdger
ID: 12220851
PS - Im coding in VB.NET
0
 
LVL 15

Assisted Solution

by:praneetha
praneetha earned 50 total points
ID: 12220899
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
ID: 12220930
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
AerosSaga earned 75 total points
ID: 12220950
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
ID: 12220993
r u adding datagrid dynamically....

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

0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12220996
0
 
LVL 1

Author Comment

by:BlueEdger
ID: 12221076
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
ID: 12221108
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
ID: 12221132
No, same error
0
 
LVL 17

Expert Comment

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

Author Comment

by:BlueEdger
ID: 12221168
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
ID: 12221170
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
ID: 12221184
check your ordinals then, are there invisible columns?
0
 
LVL 1

Author Comment

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

Expert Comment

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

Author Comment

by:BlueEdger
ID: 12221264
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
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12221267
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
ID: 12221286
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
ID: 12221297
Yes it is
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12221305
are you recreating said dynamic columns on postback?
0
 
LVL 1

Author Comment

by:BlueEdger
ID: 12221312
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
ID: 12221319
Im going to change it to bound columns
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12221347
  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
ID: 12221357
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
ID: 12221369
TextBOx textbox1=(TextBOx)e.Item.cells(0).FindControl("Textboxname");

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

Author Comment

by:BlueEdger
ID: 12221387
In VB?
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12221403
just however you would typecast in VB...
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12221427
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
ID: 12221507
Have to go now, thanks for your help guys, will continue with this tomorrow
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12221582
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
ID: 12224880
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
ID: 12225787
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
ID: 12229892
> 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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