Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 517
  • Last Modified:

Multiplying Columns in a DataGrid

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
BlueEdger
Asked:
BlueEdger
  • 14
  • 11
  • 6
  • +1
2 Solutions
 
BlueEdgerAuthor Commented:
PS - Im coding in VB.NET
0
 
praneethaCommented:
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
 
BlueEdgerAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
AerosSagaCommented:
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
 
praneethaCommented:
r u adding datagrid dynamically....

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

0
 
AerosSagaCommented:
0
 
BlueEdgerAuthor Commented:
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
 
AerosSagaCommented:
what happens if you try this:

 e.Item.Cells(1).Text = CDbl(e.Item.Cells(0).Text) * CDbl(e.Item.Cells(3).Text))
0
 
BlueEdgerAuthor Commented:
No, same error
0
 
AerosSagaCommented:
check the quick watch for those two expressions and see what values you are getting
0
 
BlueEdgerAuthor Commented:
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
 
AerosSagaCommented:
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
 
AerosSagaCommented:
check your ordinals then, are there invisible columns?
0
 
BlueEdgerAuthor Commented:
Can I stop it trying to do the calculation on the header row?
0
 
AerosSagaCommented:
why are you trying to do it in the header anyway?
0
 
BlueEdgerAuthor Commented:
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
 
AerosSagaCommented:
If you want the in the header/footer check here:
http://aspnet.4guysfromrolla.com/articles/020503-1.aspx

Aeros
0
 
BlueEdgerAuthor Commented:
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
 
AerosSagaCommented:
Yes it is
0
 
AerosSagaCommented:
are you recreating said dynamic columns on postback?
0
 
BlueEdgerAuthor Commented:
Havent got as far as postback, at the moment just have binddata function and then this itemdatabound function on page load.  
0
 
BlueEdgerAuthor Commented:
Im going to change it to bound columns
0
 
praneethaCommented:
  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
 
BlueEdgerAuthor Commented:
I just cant get the value of the textbox.  Is it not e.Item.cells(0).Text if its in a textbox?
0
 
praneethaCommented:
TextBOx textbox1=(TextBOx)e.Item.cells(0).FindControl("Textboxname");

try that...and access...using textbox1.text
0
 
BlueEdgerAuthor Commented:
In VB?
0
 
praneethaCommented:
just however you would typecast in VB...
0
 
praneethaCommented:
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
 
BlueEdgerAuthor Commented:
Have to go now, thanks for your help guys, will continue with this tomorrow
0
 
ThogekCommented:
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
 
BlueEdgerAuthor Commented:
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
 
AerosSagaCommented:
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
 
ThogekCommented:
> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 14
  • 11
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now