Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataRepeater VirtualMode Anomaly

Status
Not open for further replies.

huggyboy

Programmer
Feb 7, 2003
108
GB
I have discovered that a datarepeater object running in virtualmode set to true (ie not bound to any data source) doesnt keep all the items that are added to it programmatically.
A demonstration - create a windows application project using vb - on the form create a datarepeater and set virtual mode to true, set the size to 300 wide by 100 high (see significance later).
Add a text box to it in the dataitem and set the item template AutoSize mode to growandshrink and drag the textbox to the top of the itemtemplate and resize the itemtemplate to fit the textbox. Add a button on the form (not the repeater) and put the code below on the click event.
Dim intwork As Integer
intwork = DataRepeater1.Controls.Count
DataRepeater1.AddNew()
DataRepeater1.CurrentItem.Controls("textbox1").Text = Str(intwork + 1)

Run the program - repeatedly click on the button - items 1,2,3,4,5,6,7 are created then 7 again!!! scrolling to the top of the list shows that 1,2,3 have disappeared and scrolling back 6 and 7 have been deleted but the second 7 hasnt.
Anybody see where i have gone wrong here? Or is it just the way it is because the repeater object hasnt anywhere to keep the data? Anyone discovered any work around, binding to some kind of data object maybe (other than a programming fix (ie keeping the data in an array and refreshing it)?
A similar example bound to a database table works just fine. Thanks in anticipation that some clever people respond.
 
Adding unbound controls to a bound dataitem and trying to set the values of the unbound controls produces even wackier results - with a datarepeater that is sized to show 5 records the code below creates an almost random pattern based on 0,1,2,3,4,Label1 depending how you scroll up & down!!! going back to the top the first entries are now 0,label1,label1,1,4 again depending how it was scrolled

Dim intkount = 0
For Each item In DataRepeater2.Controls
item.controls("label1").text = Str(intkount)
intkount += 1
Next
I presume this is caused by the drawitem event triggered and (seemingly) writing something it thinks should be in the unbound item
 
Think I've almost solved it - going back to my first example
1) controls.count gives number of visible items datarepeater1.ItemCount gives the total in the repeater object so intwork = DataRepeater1.ItemCount gets the value i want
2) seems to be an event ItemValueNeeded which needs to be trapped when the data is populated from an unbound datarepeater - may be the solution to populate the item in here presumably fired when the addnew method is used
 
Sorted - on the button need
DataRepeater1.AddNew()
on the DataRepeater1_ItemValueNeeded event handler
Select Case e.Control.Name
Case "TextBox1"
e.Value = (e.ItemIndex + 1).ToString
Case "Label1"
e.Value = (e.ItemIndex + 1).ToString & "VN"
End Select
(have put a text box & label on the repeater item definition)
Bother - I will have to re-write my code to intercept the ItemValueNeeded event instead of relying on altering values in datarepeater.controls directly - only effects the ones that are visible - bother again.
Maybe this post will prevent the aggro I gone through for someone else
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top