Datarepeater added item disappears
-
Friday, June 26, 2009 8:16 PM
No one answered my original post so I'll try asking the same question differently. I'm using VS2008, vb.net, and windows forms. I have a datarepeater control that when a new item is added, it disappears if I click on another row in the datarepeater. There is just one text box in the control.
These are the only two datarepeater events I've coded for.
Private Sub DataRepeater1_DrawItem(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles LicensesDataRepeater.DrawItem Dim currItem As DataRowView = bs.Item(e.DataRepeaterItem.ItemIndex) DirectCast(e.DataRepeaterItem.Controls("txtLicense"), TextBox).Text = _ currItem("License").ToString End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click LicensesDataRepeater.AddNew() End Sub
Am I missing something obvious or important? Thanks for any help or advice.
All Replies
-
Wednesday, June 17, 2009 8:17 PM
Using VS2008, vb.net and windows forms. I've got a datarepeater control that works fine except when adding new rows. If there is more than one row currently displaying, and I add an item, the newly added item disappears if I move to another row. It just leaves a blank row. However, if I close the form and reopen it, the newly added item appears in the dr control. Is there some way to keep this from happening?
- Merged by Martin Xie - MSFT Friday, July 03, 2009 7:25 AM Merge it to keep them in the same topic.
-
Monday, June 22, 2009 1:15 PMNobody has had this problem???
-
Friday, July 03, 2009 6:48 AMModeratorHello, It seems to me the newly added row is not refreshed from the datasource.
How do you create the databinding with the datarepeater? -
Friday, July 03, 2009 7:04 AMModerator
I played with it following the walkthrough in msdn here http://msdn.microsoft.com/en-us/library/cc488278.aspx.
It seems works fine to me.
It may help if you post the full project. -
Friday, July 03, 2009 3:12 PMModerator
Thanks Martin, the question is better now but I still cannot reproduce it.
Kris, I would love to help you here if you can keep posting with more info. :) -
Monday, July 06, 2009 6:58 PM
John, here is all my code for this datarepeater control:
Public Class Licenses Dim bs As New BindingSource Dim connLicenses As SqlConnection = _ New SqlConnection("server = SQLTST; database = 'PGE - Activity'; Integrated Security = SSPI") Dim strSQL As String = "" Dim daLicenses As SqlDataAdapter Dim objCommand As SqlCommand Dim dt As New DataTable Private Sub DataRepeater1_DrawItem(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles LicensesDataRepeater.DrawItem Dim currItem As DataRowView = bs.Item(e.DataRepeaterItem.ItemIndex) DirectCast(e.DataRepeaterItem.Controls("txtLicense"), TextBox).Text = _ currItem("License").ToString End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click LicensesDataRepeater.AddNew() End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click If LicensesDataRepeater.CurrentItem.Controls("txtLicense").Text.Trim.ToString <> "" Then objCommand = New SqlCommand objCommand.Connection = connLicenses objCommand.CommandText = "spLicense_Insert" objCommand.CommandType = CommandType.StoredProcedure objCommand.Parameters.Add("@PeopleID", SqlDbType.Int, 4).Value = intID objCommand.Parameters.Add("@License", SqlDbType.VarChar, 15).Value = _ LicensesDataRepeater.CurrentItem.Controls("txtLicense").Text.Trim.ToString If connLicenses.State = ConnectionState.Closed Then connLicenses.Open() End If objCommand.ExecuteNonQuery() connLicenses.Close() MsgBox("Click on OK to continue.", MsgBoxStyle.OkOnly, "License saved") Else MsgBox("Click on OK to continue.", MsgBoxStyle.OkOnly, "Cannot leave license field blank") End If btnDelete.Visible = True End Sub Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click If LicensesDataRepeater.CurrentItem.Controls("txtLicense").Text.Trim.ToString <> "" Then objCommand = New SqlCommand objCommand.Connection = connLicenses objCommand.CommandText = "spLicense_Delete" objCommand.CommandType = CommandType.StoredProcedure objCommand.Parameters.Add("@PeopleID", SqlDbType.Int, 4).Value = intID objCommand.Parameters.Add("@License", SqlDbType.VarChar, 15).Value = _ LicensesDataRepeater.CurrentItem.Controls("txtLicense").Text.Trim.ToString If connLicenses.State = ConnectionState.Closed Then connLicenses.Open() End If objCommand.ExecuteNonQuery() connLicenses.Close() LicensesDataRepeater.RemoveAt(LicensesDataRepeater.CurrentItemIndex) MsgBox("Click on OK to continue.", MsgBoxStyle.OkOnly, "License deleted") Else MsgBox("Click on OK to continue.", MsgBoxStyle.OkOnly, "Cannot leave license field blank") End If End Sub Private Sub Licenses_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load LicensesDataRepeater.BeginResetItemTemplate() For Each ctrl As Control In LicensesDataRepeater.ItemTemplate.Controls If TypeOf ctrl Is Label Then CType(ctrl, Label).DataBindings.Clear() CType(ctrl, Label).Text = "" Else If TypeOf ctrl Is TextBox Then CType(ctrl, TextBox).DataBindings.Clear() CType(ctrl, TextBox).Text = "" End If End If Next strSQL = "SELECT * FROM tblCombined_Licenses WHERE PeopleID = " & intID daLicenses = New SqlDataAdapter(strSQL, connLicenses) daLicenses.Fill(dt) bs.DataSource = dt LicensesDataRepeater.EndResetItemTemplate() LicensesDataRepeater.DataSource = bs If bs.Count = 0 Then btnDelete.Visible = False Else btnDelete.Visible = True End If End Sub End ClassI don't know if this helps but a main form displays demographics for a medical professional. This form displays one or more licenses for that medical professional and is opened by clicking on the "Licenses" button on the main form. There are 3 buttons on this form: "Add New", "Delete", and "Save". If the user wants to add a new license, they click on Add New and a blank row appears in the datarepeater. They type in the new license and click on Save. The database is updated correctly. The new row stays visible in the datarepeater unless the user happens to click on another row. Then the new row disappears. If the user redisplays the medical professional, all licenses are displayed correctly. They can click from row to row and no data disappears. It seems to be just a "freshly added" row that causes the problem.
I have other forms where I use a datarepeater and it appears that all of them have this disappearing problem.
Thank you for your help.- Edited by kris_hood Monday, July 06, 2009 6:59 PM forgot to add something
-
Monday, July 06, 2009 10:27 PMModerator
I can repro your issue with a master-detail relationship setup but not sure this is your case.
Some questions:
1) With the following step, does it still repro?
Add new row,
Click on save,
Click on another row.
2) What happens if you remove the mainform and only work on the License form?
3) How is your dt defined? Is it part of the dataset? Is there any master-detail relationship setup here?
Some code in the main form related to the dataset and Binding Source will be helpful.
4) Question on your issue: "The newly added item disappears if I move to another row. It just leaves a blank row"
Is the row still there? i.e. it is not totally dispear, just the content become blank, or the row is gone.
-
Wednesday, July 08, 2009 1:04 AMThe code you have posted works fine, please be more descriptive..
Hector Minaya
Microsoft Visual Basic MVP | Speaker INETA Latam | MCSD | MCT | MCTS : SQL Server
twitter: http://twitter.com/HectorMinaya
Looking for SEO Tools? -
Wednesday, July 08, 2009 6:58 PM
Answering your questions:
1) Yes, the newly added data in the row disappears and the row is blank in the datarepeater.
2) Removed the main form, just worked with Licenses. Same problem
3) Maybe this is the problem? The dt in the main form is a SQL view combining several tables together to provide the demographic info on the main form. Among these tables is the main demographics table. It has a one-to-many relationship with the table containing the Licenses. The Licenses table is not included in the view. So the view is used as the dt in the main form but the Licenses table is used as the dt for the datarepeater. I can't create a relationship between a view and a table.4) The content of the datarepeater row becomes blank. The data disappears leaving a blank row. If I close the project then reopen, the data in the datarepeater row appears just fine. So the database gets updated just fine. It's just the datarepeater control that won't display it.
Thanks. I really appreciate your help with this. -
Wednesday, July 08, 2009 9:35 PMModeratorProbably the issue in on 3).
Could you dump the contents in the BindingSource (i.e. all of bs.Item) and see if the bs contains a new item, if so, what is the content?
-
Friday, July 10, 2009 7:37 PMI'm sorry, I'm not sure how to do that. I inserted this code in the btnSave_Click event and set a breakpoint.
Dim index As Integer
Dim value As Object
value = bs.Item(index)
I tried to display the value by using ?value.tostring but get a message that it has a value of Nothing.
So I moved the coding to DataRepeater1_DrawItem event but got the same result -
Saturday, July 11, 2009 6:49 PMModerator
Opps, never tried myself before (sorry), so it should be bs.Item.Item. (The bs.Item is the DataRowView), sample code like this.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each drv As DataRowView In Me.CategoriesBindingSource.List
Debug.Print(drv.Item(0).ToString())
Debug.Print(drv.Item(1).ToString())
' etc.
Next
End Sub -
Monday, July 13, 2009 3:33 PMOK, using your code I dumped out the contents of the binding source. Very interesting results.
1. I entered a new license and clicked on Save. Then dumped out the bindingsource. New license not in bs.
2. Staying on the same person, I then clicked on Add to add another new license and the previously added license disappeared....just left a blank row in the datarepeater. Then dumped out the bs, neither newly added licenses were in the bs.
3. Navigated to another person, then back to this person. New licenses appear in datarepeater. Dump of bs displays both new licenses.
Thanks for your help. -
Tuesday, July 14, 2009 7:21 PMModeratorAh ha! I think I know your problem now!
In your save method, you need to add code like this:
Me.Validate()
bs.EndEdit()
If you follow Visual Studio's walkthrough: http://msdn.microsoft.com/en-us/library/cc425007(VS.80).aspx
you will get to this sample code for save.
Hope this is the answer! :)
John -
Thursday, July 16, 2009 5:33 PM
Sorry, no go. I put your code in the Save_Click event but with no luck. Same issues. Looking thru the walkthrough you referred me to, and finding some other solutions on the web, I see that a BindingNavigator is used. I am not using one and wonder if I should. Also a TableAdapterManager is used along with the b.n. I don't seem to have that method available to me.
Should I redesign this form and use a b.n.? It appears that it is used only for data-bound controls and I'm using unbound. But this is just not working for me so I'm willing to try it.
Thanks. -
Friday, July 17, 2009 4:51 AMModeratorHmm, not sure, you can try the way in the walkthrough. The TableAdapterManager should not be related to this issue though.
-
Tuesday, December 21, 2010 1:07 AMhi, All
you find the error using "dataerror" event of datarepeater control
Good luck
adrua

