?
Solved

Saving changes to datagridview

Posted on 2007-07-24
8
Medium Priority
?
355 Views
Last Modified: 2010-04-23
I have a datagridview on my form that displays available units based on an access query. The access query is based on two tables, one with the available unit information and one with the shift information broken down into three shifts. The shifts are designated with a 1,2 or 3.
1 - 2300-0659
2 - 0700-1459
3 - 1500-2259

This way the form knows what to show based on this in the shift time field of the query
Format(Now(),"h")

My problem I have is I copy the information into a second datagridview which shows the assigned units using this code

Dim dr As DataRow = Me._CAD_050107DataSet.Tables("assignedvehicles").NewRow
        'Put values into row
        dr(1) = EventNumber.Text
        dr(2) = Datagridview1.CurrentRow.Cells(1).Value.ToString
        dr(3) = Datagridview1.CurrentRow.Cells(3).Value.ToString
        dr(4) = Datagridview1.CurrentRow.Cells(4).Value.ToString
        Me.Datagridview1.CurrentRow.Cells(7).Value = "No"
        Me.Datagridview1.CurrentRow.Cells(7).Style.BackColor() = Color.Red



        'and so on, making sure you put values in any ...
        '... fields/columns that do not allow null
        Me._CAD_050107DataSet.Tables("assignedvehicles").Rows.Add(dr)

In doing so I change one of the field values to "No" and the background to red.
Me.Datagridview1.CurrentRow.Cells(7).Value = "No"
        Me.Datagridview1.CurrentRow.Cells(7).Style.BackColor() = Color.Red

This works, but I have a timer on the form that refreshes the datagridview every minute because it is being used as a front end. When it refreshes it changes the "No" nack to "Yes"

I guess my real question is how do I save changes to the tableadapter when I run this code so it stays as "No". I have code later that will change back to "Yes".

I have tried something like

Me.Validate()
        Me.QueryShiftStatusBindingSource.EndEdit()
        Me.Datagridview2.EndEdit()
        Me.QueryShiftStatusTableAdapter.Update(Me._CAD_050107DataSet.QueryShiftStatus)


*****Update is not a member ..........******
0
Comment
Question by:wiswalld
  • 4
  • 3
8 Comments
 
LVL 10

Expert Comment

by:adriankohws
ID: 19561632
Why you refresh? How you refresh?

Is the dataset source changed? If not, then what's the purpose of refreshing.

If the dataset is requeried or somehow changed and you need the changes to take effect,
Then your routine making this change must be executed by your timer on timer_tick event, which is this routine below.

Dim dr As DataRow = Me._CAD_050107DataSet.Tables("assignedvehicles").NewRow
        'Put values into row
        dr(1) = EventNumber.Text
        dr(2) = Datagridview1.CurrentRow.Cells(1).Value.ToString
        dr(3) = Datagridview1.CurrentRow.Cells(3).Value.ToString
        dr(4) = Datagridview1.CurrentRow.Cells(4).Value.ToString
        Me.Datagridview1.CurrentRow.Cells(7).Value = "No"
        Me.Datagridview1.CurrentRow.Cells(7).Style.BackColor() = Color.Red
0
 
LVL 10

Expert Comment

by:adriankohws
ID: 19561638
You don't update this way, you can update to your dataset using dataadapter.
0
 
LVL 13

Author Comment

by:wiswalld
ID: 19561671
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.NewCADTableAdapter.Fill(Me._CAD_050107DataSet.NewCAD)
        Me.Datagridview1.Refresh()

    End Sub


In the form load event
Me.Timer1.Interval = 100000
        Me.Timer1.Enabled = True

The reason for the refresh, especially for datagridview1, is there are numerous people access the program at the same time and if one makes a change it will reflect on all computers.


Lets say I execute this on my computer, you will see it on your computer after the refresh

Dim dr As DataRow = Me._CAD_050107DataSet.Tables("assignedvehicles").NewRow
        'Put values into row
        dr(1) = EventNumber.Text
        dr(2) = Datagridview1.CurrentRow.Cells(1).Value.ToString
        dr(3) = Datagridview1.CurrentRow.Cells(3).Value.ToString
        dr(4) = Datagridview1.CurrentRow.Cells(4).Value.ToString
        Me.Datagridview1.CurrentRow.Cells(7).Value = "No"
        Me.Datagridview1.CurrentRow.Cells(7).Style.BackColor() = Color.Red

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Author Comment

by:wiswalld
ID: 19561674
The basic problem is I need to save changes made to datagridview one but I don't think this is possible since it is based on a query. Maybe I should use a table and write a query in the data designer that matches my access query?
0
 
LVL 13

Author Comment

by:wiswalld
ID: 19561687
This code copies certain cells from a selected row in datagridview1 to datagridview2

Essentially taking a unit from the availableunits datagridview and assigning it to the assigned units datagridview.

Dim dr As DataRow = Me._CAD_050107DataSet.Tables("assignedvehicles").NewRow
        'Put values into row
        dr(1) = EventNumber.Text
        dr(2) = Datagridview1.CurrentRow.Cells(1).Value.ToString
        dr(3) = Datagridview1.CurrentRow.Cells(3).Value.ToString
        dr(4) = Datagridview1.CurrentRow.Cells(4).Value.ToString
        Me.Datagridview1.CurrentRow.Cells(7).Value = "No"
        Me.Datagridview1.CurrentRow.Cells(7).Style.BackColor() = Color.Red
0
 
LVL 13

Author Comment

by:wiswalld
ID: 19561690
Me.Datagridview1.CurrentRow.Cells(7).Value = "No"
        Me.Datagridview1.CurrentRow.Cells(7).Style.BackColor() = Color.Red

When I do that I change cell (7) from "Yes" to "No" meaning the unit is not available, and I change the cell background to red.
0
 
LVL 10

Expert Comment

by:adriankohws
ID: 19563898
Although there's way to update from the value "Yes" to "No" to datatable, there's no way you can update the formatting, so only way is to put your copy routine from the datagridview1 to your datagridview2 in your timer event which includes the formatting. That will work.
0
 
LVL 34

Accepted Solution

by:
Sancler earned 2000 total points
ID: 19564441
That your code in the timer is working to refresh DataGridView1 indicates that

        Me.NewCADTableAdapter.Fill(Me._CAD_050107DataSet.NewCAD)

is updating the table on which the datagridview is based.  So it looks as though the code you need to save the data from that back to the relevant database table should be

        Me.NewCADTableAdapter.Update(Me._CAD_050107DataSet.NewCAD)

rather than

        Me.QueryShiftStatusTableAdapter.Update(Me._CAD_050107DataSet.QueryShiftStatus)

As for changing the backcolor of the datagridview cell depending on whether the value in it "Yes" or "No", you could automate that in, for instance, a DataGridView.CellValueChanged Event.

Roger
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question