Link to home
Start Free TrialLog in
Avatar of Benvor
BenvorFlag for South Africa

asked on

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
Avatar of ApexCo
ApexCo
Flag of United States of America image

Drop it in the OnRowDataBound event.

As each row is populated from the database, it will execute your code and perform your calculation.
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,,..
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....
Avatar of Benvor

ASKER

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

Avatar of Benvor

ASKER

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

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



Avatar of Benvor

ASKER

That doesn't work either. I've seen now that when I debug, that it doesn't even enter that piece of code.
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.
Avatar of Benvor

ASKER

There isn't a Itembound in the dropdown list.
hey....is it dropdownlist...or Repeater control...little bit confusing..what control you are using...
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
Avatar of Benvor

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Deathrace
Deathrace
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Benvor

ASKER

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