Question Simple datagrid field change

zendog1960

Member
Joined
Jan 9, 2008
Messages
19
Programming Experience
Beginner
I know this sounds simple to the majority of the database gurus out there but I am finding it difficult to find anything related.

I have a form which contains a datagridview. what I want to do by push of a button is set the field "OnHand" either to default or 0. The 'either or' is really for you guys. Which ever one works the best in terms of reducing code or functionality is obviouls prefered.

Once the 'OnHand' Fields in every record has changed then I would like to same all the data back to the database'

I am connecting to the database as follows:

Form Load Event
VB.NET:
Me.StuffTableAdapter.Fill(Me.StuffDataSet.Stuff)

The button is named btnClear

This is more than likely very simple I just cannot find anything that discusses this kind of thing. Any and all help are greatly appreciated...
 
It is very simple. You have a DataTable, which is a collection of DataRows. Each of those DataRows has an OnHand property. You loop through the DataTable and set the OnHand property of the DataRow to whatever value you want. I would assume that you know how to write a For Each loop.
 
I have done many 'For Each' loops but always with simple dim variabled but nothing where I would need to point to a datatable. And I don't really have a "DataTable" I think. The form has the following:
  • StuffDataSet
  • StuffBindingSource
  • StuffTableAdaptor
  • TableAdaptorManager
  • StuffBindingNavigator

Since the Navigator is for the tool bar which of the ones left is also considered the 'DataTable inwhich you referred?

Could you give me an example?

Thanks for your help by the way.
 
vis, in a dataset called StuffDataSet (the name of the class), the form instance of which is called _stuffDataset (after I drag a dataset to a form I always rename it so that it doesnt use its class name as its instance name - after all I never write Dim String as String), that contains a table called StuffDatatable:

VB.NET:
For Each ro as StuffDatatableRow in Me._stuffDataset.StuffDatatable
  ro.OnHand = 0
Next

Remember, you wrote a question that was effectively:
"How do I loop through a datagridview and set all the cels to a value of BLAH in a column of BLAH?"

.. this is quite the wrong way of thinking about it. I know that you can "see the datain" the datagridview, just like looking through the window of a shop lets you see what the shop sells.. but if you wanted something from the shop, you would go into the shop and get it. SImilarly the datagridview lets you see the contents of a datatable (and it lets the user use the mouse and keyboard to interact with it, but youre a programmer, not a user) but if you want the contents of the datatable you should always go to it directly.

Read up on MVC (model view controller) - a model holds data, a view shows the data, a controller (in this case your button) provides a way for the data to be manipulated. Sometimes V and C are combined - a textfield that shows and allows editing of data but to edit all the records in a data set you wouldnt have your program edit the text box, then programmatically click the NEXT button, then edit the text box...

..you just go straight to the model and edit that. The View updates to show the edits you just made (the shop window changes when you buy and remove something visible through it)
 
Last edited:
cjard, thanks for the reply.

Here is what I put:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        For Each ro As StuffDataSet In Me.StuffDataSet.StuffDataTable
            ro.OnHand = 0
        Next

End Sub

But I get an error that states
"StuffDataTable is a type in Inventory.StuffDataSet and cannot be used as an expression"

When I typed "StuffDatatableRow" in the first part of the For loop I got the it wasn't defined.

Are there some dims I should be declaring here? I guess I still haven't got the link to how this all is supposed to go together....

Any other help would be greatly appreciated...
 
ok here is what I got. Does this look reasonable?

VB.NET:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If MsgBox("Are you sure you want to do this? This action is irreversable!!", vbYesNo, "Confirmation") = vbNo Then
            MsgBox("Action Cancelled", MsgBoxStyle.Exclamation, "Confirmation")
        Else
            For Each row As DataGridViewRow In Me.StuffDataGridView.Rows
               row.Cells.Item(3).Value = 0
            Next
        End If
End Sub

It works so I must have done it right. To make this a bit more user friendly, can I add the following code to have it update the database from the grid:

VB.NET:
        Me.Validate()
        Me.StuffBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.StuffDataSet)

Thoughts and suggestions always welcome!!!
 
zendog1960 said:
Me.StuffTableAdapter.Fill(Me.StuffDataSet.Stuff)
Underlined, there's your typed DataTable instance.
So most likely the loop will be like this:
VB.NET:
For Each row As StuffDataSet[U].StuffRow[/U] In Me.StuffDataSet.Stuff
    row.OnHand = 0
Next
can I add the following code to have it update the database from the grid:
Bound grids store the data in data source (the table), so when you input in grid as user or programmatically those values end up in the table anyway, and updating the dataset-tables sends changes to database. Validating or ending UI edit mode is not necessary when you have directly changed table values.
 
But I get an error that states
"StuffDataTable is a type in Inventory.StuffDataSet and cannot be used as an expression"

This is why I wrote:

vis, in a dataset called StuffDataSet (the name of the class), the form instance of which is called _stuffDataset (after I drag a dataset to a form I always rename it so that it doesnt use its class name as its instance name - after all I never write Dim String as String), that contains a table called StuffDatatable


StuffDataSet is a type, like String, or Date, or File, or Button.. Something you can make a New one of. When you have a dataset on a form, visual studio made a New one of it already.. but it called it the same name as the type. Why it does this, I have no idea, but that's why I rename it (you don't call your Button "Button", etc.. Button is created as Button1, which at least is distinctive from the type name)

If youre not sure what a Type is, please refer to wikipedia Object Oriented Programming article - it explains in more depth


When I typed "StuffDatatableRow" in the first part of the For loop I got the it wasn't defined.
Point to the wiggly line underneath it and a helper appears with suggestions. Either fully qualify the type name: YourProjectNamespace.StuffDataSet.StuffDataTableRow
Or Imports it at the top of the code

Are there some dims I should be declaring here? I guess I still haven't got the link to how this all is supposed to go together....
Dims are not the same as improts. Dims reserve space in memory for instances of Types. Types have to be known to the compiler at the point the Dim is incountered

Dim b as Button 'reserves space for a button. If Button is not a known type either:

Imports System.Windows.Forms ' at the top of the code (button lives in this part of the class hierarchy)
OR:
Dim b as System.Windows.Forms.Button

If the system.windows.forms namespace is not known then you have to reference the DLL that holds it. A reference is NOT an import..
 
It works so I must have done it right.
This is never a good line of thinking I'm afraid; it implies that there is only one way to get something done and that hence must be the right way. Whoever told you to access the data via the grid itself is likely in error.. Most of the time that you manipulate data you want to do so by looking through the container that is holding the data, not the device that is showing the data

To make this a bit more user friendly, can I add the following code to have it update the database from the grid:

VB.NET:
        Me.Validate()
        Me.StuffBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.StuffDataSet)
Just.. don't do it for every row
 
Back
Top