|
I'm a newbie and I'm trying to use VB.NET 2005 beta 2. This is my problem: I have 2 forms, the first form contains a datagridview bound to a table. When I double click on a record, I open another form (detail form) bound to the same table where I can modify the record I selected. Everything works fine. The problem is: when I save data and close the detail form, I want to refresh the datagridview in the first form so that I can see the modified data. Actually the only way I succeeded to do that is using again the Fill method of the tableadapter but I hope there is a better solution.
Thanks Stefano
|
| StefanoUD Tuesday, July 05, 2005 7:55 AM |
You can do the following (I'll use the Northwind -> Shippers table as an example).
Add a DataSource to your table (Northwind -> Shippers) Drag the DataSource to your Form in Grid mode (will auto create a DataGridView) Add a CellDoubleClick event and put the following code in there (replace ShippersBindingSource with your table BindingSource)
| | Dim f2 As New Form2 f2.DataSource = Me.ShippersBindingSource(e.RowIndex) f2.ShowDialog() |
Add a new Form to the project called Form2 Drag over your Table to Form2 in Details view (use the data sources wizard) Remove the auto-generated BindingNavigator Remove the auto-generated TableAdapter F7 to code to Form2.vb Remove the two auto-generated events (Load and Save) Hook the FormClosing event on Form2 and add the following (replace with your BindingSource): | | Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Me.ShippersBindingSource.EndEdit() End Sub |
Add the following property to Form2 (below the above handler): | | Public Property DataSource() As Object Get Return Me.ShippersBindingSource.DataSource End Get Set(ByVal value As Object) Me.ShippersBindingSource.DataSource = value End Set End Propert |
F5 Note there are other ways to do this without having to remove components or that don't require the DataSet on the second form - but this is the easiest way from the designer perspective. Joe Stegman The Windows Forms Team Microsoft Corp. This posting is provided "AS IS" with no warranties, and confers no rights.
|
| Joe Stegman Wednesday, July 06, 2005 10:17 PM |
Stefano, you could try DataGridView.Refresh() or you can rebind to the data source;
DataGridView1.SuspendLayout DataGridView1.DataSource = Nothing DataGridView1.DataSource = <yourDataSource> DataGridView1.ResumeLayout
Hope that helps.
Jack |
| DotNetRules Tuesday, July 05, 2005 8:26 AM |
Hi Jack, sorry but none of your solutions do work. The only way I succeeded in refreshing data is: GridForm.NominativoTableAdapter.Fill(GridForm.MyProvaDataSet.Nominativo)
but I think refilling the table adapter is a stupid way to obtain a refresh of a (single) record. Moreover it is slow and it (obviously) moves to the first record of the table.
Stefano |
| StefanoUD Tuesday, July 05, 2005 8:47 AM |
Hi Stefano,
In the new form update the data in the database through the dataset and then on the previous form rebind the datagridview to the dataset. This should save your database query execution and should also reflect the new changes.
Regards, Kunal
|
| Kunal Yadav Tuesday, July 05, 2005 9:04 AM |
Sorry Kunal I cannot understand how to do that. When you write me to rebind the datagridview I suppose you are suggesting me to do the same thing DOTNETRULES suggested, but it does not work. Or do you mean something different? This is what I do to update data in the database: Private Sub Anagrafica_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Me.NominativoBindingSource.EndEdit() Me.NominativoTableAdapter.Update(Me.MyProvaDataSet.Nominativo) 'HERE I NEED TO INSERT THE COMMAND TO UPDATE DATA IN 'GRIDFORM.NOMINATIVODATAGRIDVIEW End Sub |
| StefanoUD Tuesday, July 05, 2005 9:34 AM |
After updating the database with the dataset, set the DataSource property of the datagridview in the other form to point to this dataset's corresponding table. Then do a Datagridview.Refresh()
The only problem over here is how to access the same dataset in both the forms. For this purpose you can create a singleton class which has a dataset as one of its members, and then you can use the same dataset in both the forms.
I hope I am clear. |
| Kunal Yadav Tuesday, July 05, 2005 9:45 AM |
Sorry Kundal I think you didn't understand the situation. The datagrid and the detail form are already connected to the same table of the same dataset, when I double click on a record of the datagrid I open the detail form using: Anagrafica.Show() Anagrafica.NominativoBindingSource.Position = Me.NominativoBindingSource.Position
where Anagrafica is the name of the detail form. When I close Anagrafica (and the record has been modified) I need to refresh data seen in the datagridview and I already tried GridForm.DataGridView.Refresh() but nothing happens.
|
| StefanoUD Tuesday, July 05, 2005 10:14 AM |
I understood the situation Stefano. On the detail form whenever you update the data, that data has to be updated in the dataset also. This dataset is what you will use to update the final database. Now this dataset has the updated information, so all you have to do is set the datasource property of the datagridview in the datagrid form to this dataset and then do a datagridview.Refresh()
So it goes like this
1. You have a dataset say dsDetails. 2. In the datagrid form, set the datasource property of the datagridview to dsDetails.Tables(<index or name>) 3. Do a datagridview.Refresh()
4. In the onclick event of the datagridview in datagrid form, open the details form. 5. Collect the updated info and update it into dsDetails.
6. Update the database with the dsDetails 7. Repeat steps 2 and 3 after the database update is done.
In this way you dont have to fetch the data from the database again, since you already have the data in the dataset.
Just doing datagridview.refresh wont make any difference, the datasource property should point to the updated dataset |
| Kunal Yadav Tuesday, July 05, 2005 10:24 AM |
Kunal I cannot understand (and cannot make it work). | Kunal Yadav wrote: | | On the detail form whenever you update the data, that data has to be updated in the dataset also. |
|
This should send data to the dataset: Me .NominativoBindingSource.EndEdit() And this should send data from the dataset to the datatable: Me.NominativoTableAdapter.Update(Me.MyProvaDataSet.Nominativo)
So when I close the Detail form I should have updated data both in the dataset and in the database (in fact when I open again the application I can see modified data). | Kunal Yadav wrote: | In the datagrid form, set the datasource property of the datagridview to dsDetails.Tables(<index or name>)
|
|
Why shoud I connect the datagridview to the dataset table? Why I cannot use the bindingsource objects created by the wizard? |
| StefanoUD Tuesday, July 05, 2005 3:05 PM |
Binding source does not automatically refresh the data. Follow this post for that. Regards, Kunal |
| Kunal Yadav Wednesday, July 06, 2005 4:06 AM |
You can do the following (I'll use the Northwind -> Shippers table as an example).
Add a DataSource to your table (Northwind -> Shippers) Drag the DataSource to your Form in Grid mode (will auto create a DataGridView) Add a CellDoubleClick event and put the following code in there (replace ShippersBindingSource with your table BindingSource)
| | Dim f2 As New Form2 f2.DataSource = Me.ShippersBindingSource(e.RowIndex) f2.ShowDialog() |
Add a new Form to the project called Form2 Drag over your Table to Form2 in Details view (use the data sources wizard) Remove the auto-generated BindingNavigator Remove the auto-generated TableAdapter F7 to code to Form2.vb Remove the two auto-generated events (Load and Save) Hook the FormClosing event on Form2 and add the following (replace with your BindingSource): | | Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Me.ShippersBindingSource.EndEdit() End Sub |
Add the following property to Form2 (below the above handler): | | Public Property DataSource() As Object Get Return Me.ShippersBindingSource.DataSource End Get Set(ByVal value As Object) Me.ShippersBindingSource.DataSource = value End Set End Propert |
F5 Note there are other ways to do this without having to remove components or that don't require the DataSet on the second form - but this is the easiest way from the designer perspective. Joe Stegman The Windows Forms Team Microsoft Corp. This posting is provided "AS IS" with no warranties, and confers no rights.
|
| Joe Stegman Wednesday, July 06, 2005 10:17 PM |
Thanks Joe, your answer was helpful and is working on my test project. I undestood your method and your code and now I can use it. Now I have to go on and try to create the Add New Record button and then try to add one (or more) relationed tables in the Form2. Do you think it is possible (and easy) to go on with this method or is there another better way? Remember I'm a (really very) newbie and it's difficult for me understanding all these new objects and how to manipulate them, so I want to use a simple way and as many wizards as I can.
Thanks |
| StefanoUD Tuesday, July 12, 2005 8:51 AM |
I too am trying to achieve these results.
I am going to try the edit method described above but would like to know how to do the add new record approach when you have no DataRow to bind.
Stefano, I'm wondering whether you're still using VS2005 and whether you solved this problem, or if anyone else has solved it...
Cheers,
Roy |
| Corneloues Saturday, January 21, 2006 11:51 PM |
sir,
I tried your method of passing the datasource. it works. But I would like to know how the datagridview can be refreshed without passing the datasource.I don't require the dataset in my second form. Please help
Thanks in advance
jaysat
|
| satjay Saturday, February 04, 2006 5:37 AM |
Joe:
I built a dual form app that needs the navigator and other components you removed in the sample above.
Will you please give me an example using the above method, but without removing those items.
-Joo |
| Jooo Thursday, May 18, 2006 2:26 PM |
|
| Jooo Thursday, May 18, 2006 8:08 PM |
when I saw your post message, I was very excited bcos like you am also a newbie in c# and exactly what you have done is what I want to do. But the only differnce is that I intend using stored procedures that pulls data from multiple tables to achive the sameresult rather thanpulling directly from table. In addtiion, themaster form should contain other buttons that can be clicked to view,edit and new to inserteach record into the database. while the detail form view contains a save button for saving to the database.
Is it also possible to use one detail form to accomplish the above task? if possible, how?
I will appreciate it if you can post me sample codes of how you did it. C# codes will be preferred.
Thanks a Million,
Celestine |
| Celestine Wednesday, August 08, 2007 3:23 PM |
Hello, i am new to the .net world. Was hoping someone can help me. This issue is very urgent for my project. I am using vb.net 2005 . I have two frames/windows, say F1 and F2 . F1 has a DataGridView ( call it dg) and a button ( call it add_btn) . whenever the user clicks add_btn he gets F2. F2 has couple of text fields and a button. when the user clicks the button it takes all the text fields values and insert them in the data base. I managed to insert the values in the database. All I am trying to do is refresh the data grid in F1. Here is my code: Dim dbDs As DataSet = connToDb.recordsFromDB(sqlConn) F1.dbDG.DataSource = dbDs Console.WriteLine("number of rows: " & F1.dbDs.Tables(0).Rows.Count) 'we have the right number of rows after the insertion F1.DataMember = F1.dbDs.Tables(0).TableName F1.dbDG.Refresh() I failed to refresh my datagreidview Can you please help with this issue. please provide code |
| gorbag Sunday, August 23, 2009 6:09 AM |