Benvor
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
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
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("textbo x1") as TextBox;
// other control like km and previous km lets say stored in textbox2 and 3
TextBox km = e.Item.FindControl("textbo x2") as TextBox;
TextBox pkm = e.Item.FindControl("textbo x3") as TextBox;
textbox1.Text = /// perform the calculation and store the value into textbox1..
}
hope this might help,,..
{
// if textbox1 is where you want to store the distance, and this control is in repeater
TextBox textbox1 = e.Item.FindControl("textbo
// other control like km and previous km lets say stored in textbox2 and 3
TextBox km = e.Item.FindControl("textbo
TextBox pkm = e.Item.FindControl("textbo
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....
I was thinking Gridview for some reason, but you asked for DataRepeater.
Still early here on the west coast I guess....
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
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
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
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
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.
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.
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(By Val sender As Object, ByVal e As System.Web.UI.WebControls. RepeaterIt emEventArg s) Handles Repeater1.ItemDataBound
Dim textbox1 As TextBox = TryCast(e.Item.FindControl ("edtDista nce"), TextBox)
Dim km As TextBox = TryCast(e.Item.FindControl ("Kilomete rsTextBox" ), TextBox)
Dim pkm As TextBox = TryCast(e.Item.FindControl ("PrevKMTe xtBox"), TextBox)
End Sub
Protected Sub Repeater1_ItemDataBound(By
Dim textbox1 As TextBox = TryCast(e.Item.FindControl
Dim km As TextBox = TryCast(e.Item.FindControl
Dim pkm As TextBox = TryCast(e.Item.FindControl
End Sub
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
As each row is populated from the database, it will execute your code and perform your calculation.