Help Regarding refreshing dataGridView

shalan99

Active member
Joined
Jun 1, 2007
Messages
32
Programming Experience
Beginner
Hi all!

I have a VB app with SQL Server 2005 db. On my main form I have a DataGridView and a few labels for Master/Detail purposes, which are all bound to MyBindingSource (no binding navigator). I have chosen to not allow the user to delete, add or edit from the Grid for certain reasons...hence the (read-only) labels for the Details. Any adding or editing will be done via another Dialog box. Any deleting will be executed via a button above the Grid.

My add/edit dialog is a generic form, ie it has a "FormMode", so if I hit the "Add New" button it will show the form with all textboxes, dateTimePickers, etc being empty. If I select a row and hit the "Edit" button or double-click on the row, the FomMode of my dialog changes, and I pass through the primary key as an Integer. So in the Load event, I do a select based on the Integer passed in, and fill the textboxes, etc accordingly. Sorry if Im being elementary here with my descriptions, but just want to paint a complete picture.

My question is this:
Whether I delete, add or edit any of the rows' data, I want to know what is the most efficient way of refreshing the datagridview? I know that MyBindingSource.ResetBindings(false) won't work, as the input controls on my dialog form are unbound. Anyway after the line of code that i use to delete a row, I call a procedure where i clear the tableadapter and then "re-fill" it. I do a similar thing for Adding and Editing. This method does work now as I have only 5 or so rows of data that I'm testing with. But my Database table is going to grow. So performance will become an issue if I have to keep making the Fill method call. Is there a better, more efficient way of doing what I want?

FYI: For editing, I am opening the above-mentioned dialog box and my code flows as:

VB.NET:
Dim myDialog as new frmAddEdit
With myDialog
[INDENT].formMode = myMode.Edit[/INDENT]
[INDENT].rowID = DataGridView.SelectedRows.Item(0).Cells(0).Value[/INDENT]
[INDENT].ShowDialog()[/INDENT]
End With
myTableAdapter.ClearBeforeFill = True
myTableAdapter.Fill(myDataSet.myTable)

I know this is not very efficient, so please could someone shed some light on this for me. Cjard, if u r reading this post also, I hold in high regard any advice u have.

Thank u very much!

rgdz
Shalan
 
If I select a row and hit the "Edit" button or double-click on the row, the FomMode of my dialog changes, and I pass through the primary key as an Integer. So in the Load event, I do a select based on the Integer passed in, and fill the textboxes, etc accordingly.
ok. What I dont get is:

You download a data row from a DB. you show it. When the user wants to edit it, you take the PK of the row, open a new window, download the row again, edit the copy, save the copy and nopw want to refresh the original?

Why dont you just change the original? Pass the original BIndSOurce to the modal dialog, and set the bindsource of the modal to = the bindsource of the original. SUddenly all the modal fields will be editing the original row

Whether I delete, add or edit any of the rows' data, I want to know what is the most efficient way of refreshing the datagridview?
Edit in situ rather than a copy, and it will show up anyway.

I know that MyBindingSource.ResetBindings(false) won't work, as the input controls on my dialog form are unbound.
Bind them

Anyway after the line of code that i use to delete a row, I call a procedure where i clear the tableadapter and then "re-fill" it.
Why? A bindingsource wont let deleted rows show up, so as soon as you call .Delete() on a row in a datatable, it disappears from the grid.
Mayb you didnt call .Delete() on the row, but delteed it directly from the db.. then you needed to refill


So performance will become an issue if I have to keep making the Fill method call.
Downloading all rows from a table is nearly always not what you want

Is there a better, more efficient way of doing what I want?
More efficient way of downloading all rows? No. CHange the requirement to show all rows on screen from "yes" to "no". This may involve coercing your manager


.rowID = DataGridView.SelectedRows.Item(0).Cells(0).Value

Try not to use a grid as though it is a store of data. Instead use a bindingsurce. Whatever row is highlighted in the grid, is the .Current of the bind source


I know this is not very efficient, so please could someone shed some light on this for me.

Have a sensible FillByXXX query on the tableadapter so only a few rows are downloaded
Use a bindingsource
Add, edit and delete the original rows
Pass reference of the bindingsource to another form, and tell and bindsource on that form to use the passed bindsource as a datasource, to show data from one form on another
Do not use grids like tabular stores of data; they are merely for showing data. Always get the data by going underneath
Edit original data, not copies​
 
Hi Cjard and thanx for the response.

Sorry about my delay in responding. I had no internet connectivity so this is my first chance to respond.

You know, I knew all along that the way I was working with data in my applications was wrong. to be honest, I've started working with WinForms in the last 3 months (no excuse tho :)) I come from a web design and development background, so I wanted to explore winForms as well.

I understand that the datagridview is for showing data and not storing it.

Always get the data by going underneath
and set the bindsource of the modal to = the bindsource of the original. SUddenly all the modal fields will be editing the original row

100%!! Based on that, I did a lot of additional reading up and found some informative training videos on correct usage of ADO.NET and how to use the bindingsource to a larger extent. My application performs a lot faster as I am now working with data on the "client side" (disconnected) and not keep having to work with live data! And now, not only can I do the things I couldn't do before using using my messed up methods, but I can also accomplish my data tasks faster! :D

Look, I know I've been a bit of a pest with my questions, but I just wanted to thank u for giving me a bit of a nudge in the right direction. In future, I can assure that my questions will be more knowledgeable and in line with current methods.

Thanx again!

regards
Shalan99
 
Back
Top