I fill in a dataset its data table and use it for binding some textboxes
con.Open()
da = New SqlDataAdapter("SELECT * FROM Item", connection)
da.Fill(ds, "Item")
cmb = New SqlCommandBuilder(da)
txtItemName.DataBindings.Add(New Binding("Text", ds.Tables("Item"), "Item_Name"))
txtItemDescription.DataBindings.Add(New Binding("Text", ds.Tables("Item"), "Item_Description"))
txtCurrentStock.DataBindings.Add(New Binding("Text", ds.Tables("Item"), "CurrentStock"))
txtUnitPrice.DataBindings.Add(New Binding("Text", ds.Tables("Item"), "UnitPrice"))
After that i give the same datatable as a datasource for the datagridview too
DGV1.DataSource = ds.Tables("Item")
Now when i want to insert a new record to the table, i clear all the current textboxes and let the user type the new information Item Name,Item Description,Current Stock and Unit Price After that, on the save button i do this coding...
Dim dr As DataRow = ds.Tables("Item").NewRow
dr("Item_Name") = txtItemName.Text
dr("Item_Description") = txtItemDescription.Text
dr("CurrentStock") = txtCurrentStock.Text
dr("UnitPrice") = txtUnitPrice.Text
ds.Tables("Item").Rows.Add(dr)
da.Update(ds, "Item")
ds.Tables("Item").AcceptChanges()
But unfortunately, when i do this, a row is added twice! It is added as the first record in the datatable (shown in the datagridview) and again as the last record too. So altogether 2 rows are inserted on to the datagridview and datatable. But strangely it is perfectly added to as the last row in the actual database :( What am i doing wrong? The only way i fix this is, programatically delete the first row each time a record is added... but i know this is not the solution and im doing something wrong :( | | Ranhiru123 Sunday, July 26, 2009 6:35 AM | OK, I am not sure how you start with the add new. Do you using a BindingSource as the datasoruce? By reading you describption,
"Now when i want to insert a new record to the table, i clear all the current textboxes and let the user type the new information Item Name,Item Description,Current Stock and Unit Price"
Here is my guess: You table does not have a primary key or unit key.
1. Your DataGirdView has the current row (focus) on the first row 2. You click a Add new button, what you are doing is simply clear all the text box. --- This is not good as all the text box is still bound to the first row in the datagridview, and the corresponing datarow. 3. You let user enter something on the text boxes. 4. User click on the save button. 5. The save button do this:
Dim dr As DataRow = ds.Tables("Item").NewRow
dr("Item_Name") = txtItemName.Text dr("Item_Description") = txtItemDescription.Text dr("CurrentStock") = txtCurrentStock.Text dr("UnitPrice") = txtUnitPrice.Text ds.Tables("Item").Rows.Add(dr) da.Update(ds, "Item") ds.Tables("Item").AcceptChanges()
Thisis what happens In step 2-3, you are changing the first row in the datagridview. ds.Tables("Item").Rows.Add(dr)-- one new row add to the bottom of the datatable da.Update(ds, "Item") -- this new row added to the database ds.Tables("Item").AcceptChanges() -- the first row is changed (like the new row), and the last row is the actual new row.
Anyway, I think you are not doing the right thing for adding new row.
Some suggestions: 1) Read some article from Microsoft DataAccess technologies here: http://msdn.microsoft.com/en-us/library/wzabh8c4.aspx I also wrote a instruction style blog here for your reference: http://jcsharp.spaces.live.com/blog/cns!7FF4CD2B1E6A7D55!328.entry?&_c02_vws=1
2) Recommend you use BindingSource in your data binding application.
Create a BindingSource,
Set the BindingSource.DataSource to the DataTable
Set your DataGridView.DataSource to the BindingSource
Bind you TextBox to the field of the same BindingSource
When Add a new item programmatically (you can sure usea BindingNavigator), call BindingSource.AddNew
Hope this help.
John - Edited byJohn Chen MS Sunday, July 26, 2009 9:40 PM
- Marked As Answer byRanhiru123 Friday, July 31, 2009 6:24 AM
-
| | John Chen MS Sunday, July 26, 2009 8:25 AM | As I replied before:
"When Add a new item programmatically (you can usea BindingNavigator as well), call BindingSource.AddNew" That is you call bs.AddNew, this will move your DGV1 to aadd new state and at the same time the textboxes will be cleared.
Have you tried this?
- Marked As Answer byRanhiru123 Friday, July 31, 2009 6:25 AM
-
| | John Chen MS Thursday, July 30, 2009 2:13 AM | Well, this is a solution but try use bs.AddNew and see. - Marked As Answer byRanhiru123 Friday, July 31, 2009 6:25 AM
-
| | John Chen MS Thursday, July 30, 2009 2:19 AM | I tried some simple app with date field, it seems work fine to me. It seems to me you should try this: txtShippingDate.DataBindings.Add(new Binding("Value", bs, "Ship_Date", true)) (I think your txtShippingDate is the DateTimePicker control, right?) Anyway, I think you should use the constrcutor with the EnableFormatting parameter set to true for your date type. See ref: http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.formattingenabled.aspxLet me know if this help.
John Chen
-- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
- Marked As Answer byRanhiru123 Friday, August 07, 2009 4:45 AM
-
| | John Chen MS Thursday, August 06, 2009 4:30 AM | Because you have the databinding setup, you do not need to have this code in the save. Dim dr As DataRow = ds.Tables("Item").NewRow
dr("Item_Name") = txtItemName.Text dr("Item_Description") = txtItemDescription.Text dr("CurrentStock") = txtCurrentStock.Text dr("UnitPrice") = txtUnitPrice.Text
ds.Tables("Item").Rows.Add(dr)
This is the cause of the duplication.
HTH,
John | | John Chen MS Sunday, July 26, 2009 6:59 AM | Im sorry! How else should i add a new record to the datatable??? :S And specifically which code should not be there | | Ranhiru123 Sunday, July 26, 2009 7:11 AM | OK, I am not sure how you start with the add new. Do you using a BindingSource as the datasoruce? By reading you describption,
"Now when i want to insert a new record to the table, i clear all the current textboxes and let the user type the new information Item Name,Item Description,Current Stock and Unit Price"
Here is my guess: You table does not have a primary key or unit key.
1. Your DataGirdView has the current row (focus) on the first row 2. You click a Add new button, what you are doing is simply clear all the text box. --- This is not good as all the text box is still bound to the first row in the datagridview, and the corresponing datarow. 3. You let user enter something on the text boxes. 4. User click on the save button. 5. The save button do this:
Dim dr As DataRow = ds.Tables("Item").NewRow
dr("Item_Name") = txtItemName.Text dr("Item_Description") = txtItemDescription.Text dr("CurrentStock") = txtCurrentStock.Text dr("UnitPrice") = txtUnitPrice.Text ds.Tables("Item").Rows.Add(dr) da.Update(ds, "Item") ds.Tables("Item").AcceptChanges()
Thisis what happens In step 2-3, you are changing the first row in the datagridview. ds.Tables("Item").Rows.Add(dr)-- one new row add to the bottom of the datatable da.Update(ds, "Item") -- this new row added to the database ds.Tables("Item").AcceptChanges() -- the first row is changed (like the new row), and the last row is the actual new row.
Anyway, I think you are not doing the right thing for adding new row.
Some suggestions: 1) Read some article from Microsoft DataAccess technologies here: http://msdn.microsoft.com/en-us/library/wzabh8c4.aspx I also wrote a instruction style blog here for your reference: http://jcsharp.spaces.live.com/blog/cns!7FF4CD2B1E6A7D55!328.entry?&_c02_vws=1
2) Recommend you use BindingSource in your data binding application.
Create a BindingSource,
Set the BindingSource.DataSource to the DataTable
Set your DataGridView.DataSource to the BindingSource
Bind you TextBox to the field of the same BindingSource
When Add a new item programmatically (you can sure usea BindingNavigator), call BindingSource.AddNew
Hope this help.
John - Edited byJohn Chen MS Sunday, July 26, 2009 9:40 PM
- Marked As Answer byRanhiru123 Friday, July 31, 2009 6:24 AM
-
| | John Chen MS Sunday, July 26, 2009 8:25 AM | Im currently trying to understand :) And i do have a Primary key for my table :) And thanx a lot for your valuable idea... i will try to code for this :) | | Ranhiru123 Monday, July 27, 2009 12:47 PM | Hello! I successfully created a binding source and bounded the text boxes to the binding source
bs.DataSource = ds.Tables("Customer")
'Data Binding
txtCusName.DataBindings.Add(New Binding("Text", bs, "Cus_Name"))
txtCusAddress.DataBindings.Add(New Binding("Text", bs, "Cus_Address"))
After that
So the datagridview's datasource is set too And to traverse through the records i use bs.MoveNext() and bs.MovePrevious() OK..so now everything is cool and traversing through the records are fine and both the items in the datagridview and textboxes are synced But how ever im stuck on how to add a new record to the database :S Do i create a DataRow and add it ? And yes i noticed that when i clear my text boxes and type the customer name on to the first textbox which is the second field (Customer ID is the first), it is edited on the datagridview too. But strangely when i am typing the customer name only this happens. Other fields do not get edited in the datagridview. Thank you in advance | | Ranhiru123 Tuesday, July 28, 2009 4:42 AM | Well i actually kind of got the whole thing incorrect! It's actually not a new row inserted at the top! Its just the first record being edited and it contains the new values! So it looks like quickly that the same value is inserted at the top and bottom, but actually the bottom row is added as well as the first one being changed! So im LOSING the values of the first record! I think i just cant let them edit the textboxes by clearing the textbox because they are already being databound? Do i have to programmatically remove the databindings and add them back? Or is there any other way!! I cant believe i really thought it was a new record being added to top!! Damn!
| | Ranhiru123 Tuesday, July 28, 2009 5:03 AM | The only solution i now have (working really well) is to clear the bindings of textboxes when user is entering the information and rebind them when the user presses the Save button. this is working really good but my heart says this is not the solution... :S | | Ranhiru123 Tuesday, July 28, 2009 7:38 AM | As I replied before:
"When Add a new item programmatically (you can usea BindingNavigator as well), call BindingSource.AddNew" That is you call bs.AddNew, this will move your DGV1 to aadd new state and at the same time the textboxes will be cleared.
Have you tried this?
- Marked As Answer byRanhiru123 Friday, July 31, 2009 6:25 AM
-
| | John Chen MS Thursday, July 30, 2009 2:13 AM | I have explained this to you before. "Thisis what happens In step 2-3, you are changing the first row in the datagridview. ds.Tables("Item").Rows.Add(dr)-- one new row add to the bottom of the datatable da.Update(ds, "Item") -- this new row added to the database ds.Tables("Item").AcceptChanges() -- the first row is changed (like the new row), and the last row is the actual new row.
Anyway, I think you are not doing the right thing for adding new row. "
| | John Chen MS Thursday, July 30, 2009 2:16 AM | Well, this is a solution but try use bs.AddNew and see. - Marked As Answer byRanhiru123 Friday, July 31, 2009 6:25 AM
-
| | John Chen MS Thursday, July 30, 2009 2:19 AM | Awesome man!!!! Thanx soooooooooooooooooooooooooooooooooooooooooooooooooo much!!!! Its working perfectly!!
| | Ranhiru123 Friday, July 31, 2009 6:24 AM | Im sorry to bother you... though everything is working fine i bind something to a DateTimePicker control... bs.AddNew() does not work :S
txtShippingDate.DataBindings.Add(New Binding("Value", bs, "Ship_Date"))
This is what i do... but when i call the bs.AddNew() it is not functioning properly :S What am i doing wrong? Traversing through the records and everything is working fine! The dates are properly displayed when next and back buttons are pressed... the problem comes when a new item is added :( | | Ranhiru123 Wednesday, August 05, 2009 8:53 AM | What is the error (if any), and what do you mean not functioning? DateTimePicker control seems not very easy for databinding. There seems a lot questions on the web. Here is a similar question I searched: http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.windowsforms.databinding/2004-12/0005.html I do not have the experience yet, but i believe there should be a solution out there.
John Chen
-- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
| | John Chen MS Wednesday, August 05, 2009 4:19 PM | Well there is no visible error! Yet the normal functioning when bs.AddNew() is called is not there... What usually happens is that when bs.AddNew() is called, all my text fields are cleared and in the datagridview (if any) the selected row becomes a new created empty row. And i can create a datarow and add it to the datatable. But when binded to a date time picker control, the text boxes are not cleared and the selected row on the datagridview stays the same (first one) and when adding a new row, the same original problem (the first line being changed, instead of a new one) is reproduced :( | | Ranhiru123 Wednesday, August 05, 2009 6:37 PM | I tried some simple app with date field, it seems work fine to me. It seems to me you should try this: txtShippingDate.DataBindings.Add(new Binding("Value", bs, "Ship_Date", true)) (I think your txtShippingDate is the DateTimePicker control, right?) Anyway, I think you should use the constrcutor with the EnableFormatting parameter set to true for your date type. See ref: http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.formattingenabled.aspxLet me know if this help.
John Chen
-- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
- Marked As Answer byRanhiru123 Friday, August 07, 2009 4:45 AM
-
| | John Chen MS Thursday, August 06, 2009 4:30 AM | Thanx mate! It works fine now with the True as a parameter! :) | | Ranhiru123 Friday, August 07, 2009 4:47 AM | Ohh im sorry to bother u again John... but this whole DataBinding is driving me crazy again :(.... To be honest, im just not getting everything done from you, k? i tried everything i could but still there seems to be a problem :S
txtOrderID.DataBindings.Add(New Binding("Text", bs, "Order_ID", True))
txtPackingType.DataBindings.Add(New Binding("SelectedItem", bs, "PackingType", True))
txtDate.DataBindings.Add(New Binding("Value", bs, "PackingDate", True))
txtNoOfPacking.DataBindings.Add(New Binding("Value", bs, "NoOfPacking", True))
txtDescription.DataBindings.Add(New Binding("Text", bs, "Packing_Description", True))
I do the same thing... now txtOrderID and PakcingType are combo boxes... txtDate is a date time picker... txtNoOfPacking is a Numeric UpDown... and txtDesription a textbox... and same thing is done when saving
Private Sub cmdSaveRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveRecord.Click
Dim dr As DataRow = ds.Tables("Packing").NewRow
dr("Order_ID") = txtOrderID.SelectedValue
dr("PackingType") = txtPackingType.SelectedItem.ToString
dr("PackingDate") = txtDate.Value.ToShortDateString
dr("NoOfPacking") = txtNoOfPacking.Value
dr("Packing_Description") = txtDescription.Text
ds.Tables("Packing").Rows.Add(dr)
da.UpdateCommand = cmb.GetUpdateCommand
da.Update(ds, "Packing")
DisableItems()
cmdSaveRecord.Visible = False
End Sub
DisableItems, just disables items so users cannot intentionally or unintentionally modify the contents.. But now, the funny part is that once the save button is pressed, a row is added twice.. to the bottom of the datagridview... TWICE, to the bottom... but to the database it is added only once... and i have noticed something. Below is when two items are added... and the coding used for that is...
txtOrderID.DataBindings.Add(New Binding("Text", bs, "Order_ID"))
txtShippingDate.DataBindings.Add(New Binding("Value", bs, "Ship_Date", True))
txtShippingType.DataBindings.Add(New Binding("SelectedItem", bs, "Ship_Type"))
txtEstimatedShipDate.DataBindings.Add(New Binding("Value", bs, "Estimated_Delivery", True))
txtLocation.DataBindings.Add(New Binding("Text", bs, "Delivery_Location"))
Notice that i have added, formattingEnabled = True to only txtShippingDate and txtEstimatedDate which are DateTimePickers...and those two are empty in the second (unwanted) row... But this is the coding i used in the Packing Form...
txtOrderID.DataBindings.Add(New Binding("Text", bs, "Order_ID", True))<br/>
txtPackingType.DataBindings.Add(New Binding("SelectedItem", bs, "PackingType", True))<br/>
txtDate.DataBindings.Add(New Binding("Value", bs, "PackingDate", True))<br/>
txtNoOfPacking.DataBindings.Add(New Binding("Value", bs, "NoOfPacking", True))<br/>
txtDescription.DataBindings.Add(New Binding("Text", bs, "Packing_Description", True))
I have used "True" for everything... but only two items are empty... i dunno whether they have any relationship :S But why is another unwanted row added to the bottom??? :S PS: I dont think there is any relationship... im doing something wrong again... this time when i added a row, the exact same row was added (with all the information) twice... :S please help me :( | | Ranhiru123 Sunday, August 09, 2009 5:38 AM |
|