Calculate between Datarepeater

Hi Experts

I have a Datarepeater on my form with a few fields that are bound to a database. The two field I am interested in is Kilometer and Previous Kilometer, because I need to calculate the distance traveled. I  have the formula to do this but I don't know where to put it. Can anyone please tell me where I should put my code so that It calculates EACH record.

Thanx in advance
untitled123.JPG
BenvorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ApexCoCommented:
Drop it in the OnRowDataBound event.

As each row is populated from the database, it will execute your code and perform your calculation.
0
DeathraceCommented:
protected void DataRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // if textbox1 is where you want to store the distance, and this control is in repeater
    TextBox  textbox1 = e.Item.FindControl("textbox1") as TextBox;
     // other control like km and previous km lets say stored in textbox2 and 3
    TextBox  km = e.Item.FindControl("textbox2") as TextBox;
    TextBox  pkm = e.Item.FindControl("textbox3") as TextBox;

     textbox1.Text =  /// perform the calculation and store the value into textbox1..    
}

hope this might help,,..
0
ApexCoCommented:
Deathrace has it right.

I was thinking Gridview for some reason, but you asked for DataRepeater.

Still early here on the west coast I guess....
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

BenvorAuthor Commented:
I have tried the DataRepeater_ItemDataBound. (Note that I'm using VB.net so I converted it from C# to vb.net using http://www.developerfusion.com/tools/convert/csharp-to-vb/) Then I get the code, that I've attached, but there is no "ItemDataBound" method for Datarepeater, so it just throws a n error "Type 'RepeaterItemEventArgs' is not declared". So I've tried all the suggested types: MeasureItemEventArgs, DrawItemEventArgs and DrawListViewItemEventArgs but none of them work.
    Protected Sub DataRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
        ' if textbox1 is where you want to store the distance, and this control is in repeater
        Dim textbox1 As TextBox = TryCast(e.Item.FindControl("textbox1"), TextBox)
        ' other control like km and previous km lets say stored in textbox2 and 3
        Dim km As TextBox = TryCast(e.Item.FindControl("textbox2"), TextBox)
        Dim pkm As TextBox = TryCast(e.Item.FindControl("textbox3"), TextBox)
 
    End Sub

Open in new window

0
BenvorAuthor Commented:
This runs without errors but it doesn't seem to do anything.
    Protected Sub DataRepeater_ItemDataBound(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs)
 
        Dim textbox1 As TextBox = TryCast(e.DataRepeaterItem.Controls("edtDistance"), TextBox)
 
        Dim km As TextBox = TryCast(e.DataRepeaterItem.Controls("KilometersTextBox"), TextBox)
        Dim pkm As TextBox = TryCast(e.DataRepeaterItem.Controls("PrevKMTextBox"), TextBox)
        edtDistance.Text = (Integer.Parse(km.Text) - Integer.Parse(pkm.Text)).ToString
    End Sub

Open in new window

0
DeathraceCommented:
well,, u did a mistake !!!

Dim textbox1 as TextBox // this line we are referencing to "edtDistance" in repeater
but if you see your last line, // edtDistance.Text what's this,, we didn't create instance to this, change this to textbox1.Text... to see the output..

might be the solution.... please try and let me know



0
BenvorAuthor Commented:
That doesn't work either. I've seen now that when I debug, that it doesn't even enter that piece of code.
0
ApexCoCommented:
The last bit of code by Deathrace looks right.

You say it's not being hit when you debug. Is it wired up correctly?

Since you are using VB, wipe the whole thing out and choose your datarepeater from the dropdown on the top left of the IDE. Then choose the itembound event from the dropdown to the right.

If you just typed up the constructor without wiring it up to the actual control on your page it won't fire.
0
BenvorAuthor Commented:
There isn't a Itembound in the dropdown list.
0
DeathraceCommented:
hey....is it dropdownlist...or Repeater control...little bit confusing..what control you are using...
0
DeathraceCommented:
If its a repeater it definetly has ItemDataBound,i tested the below code as well please check this

Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
        Dim textbox1 As TextBox = TryCast(e.Item.FindControl("edtDistance"), TextBox)

        Dim km As TextBox = TryCast(e.Item.FindControl("KilometersTextBox"), TextBox)
        Dim pkm As TextBox = TryCast(e.Item.FindControl("PrevKMTextBox"), TextBox)

    End Sub
0
BenvorAuthor Commented:
That is a "Repeater" and you are using Webcontrols. I have a "Datarepeater" from Visual Basic Power Packs on my Windows Application form and I assure you I don't see a "ItemEventArgs" in the events list
0
DeathraceCommented:
hei really sorry i thought you are using Repeater web control....so for DataRepeater we have DrawItem event which fires on every record binding.... using some thing like this

Datarepeater1_DrawItem( by val, ...)
{  
 Dim textbox1 As TextBox = TryCast(e.DataRepeaterItem.FindControl("edtDistance"), TextBox)
 Dim km As TextBox = TryCast(e.DataRepeaterItem.FindControl("KilometersTextBox"), TextBox)
 Dim pkm As TextBox = TryCast(e.DataRepeaterItem.FindControl("PrevKMTextBox"), TextBox)
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BenvorAuthor Commented:
That worked thank you. I have attached the code for anyone in the future who also want's to do calculations between datarepeater items.
    Private Sub DataRepeater1_DrawItem(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles DataRepeater1.DrawItem
        Dim textbox1 As TextBox = TryCast(e.DataRepeaterItem.Controls("edtDistance"), TextBox)
        Dim km As TextBox = TryCast(e.DataRepeaterItem.Controls("KilometersTextBox"), TextBox)
        Dim pkm As TextBox = TryCast(e.DataRepeaterItem.Controls("PrevKMTextBox"), TextBox)
        textbox1.Text = (Integer.Parse(km.Text) - Integer.Parse(pkm.Text)).ToString
    End Sub

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.