0

So I have a DBGrid filled with an SQL query. I want the query to be read only so the table isn't locked up while the program is running. The problem is when it is set to read only I can't edit the table at all.

What I want to do is be able to edit the fields on the DBGrid while in readonly mode and then update it after all at once so I don't have to leave the connection open.

I'm not really sure how to do this though. DBGrid doesn't have an OnChange event, so I can't just run an update query every time a cell is changed.

Anyone have some ideas?

1
  • Why would the table be locked up when you leave the connection open? It shouldn't. Commented Jan 24, 2013 at 1:31

1 Answer 1

3

This is the exact reason that TClientDataSet was written. It allows you to take a snapshot of data, store it in memory, update it as if it were an actual dataset, and then easily apply those updates back to the original database.

There's a tutorial here that's not too bad. You can find another one at Scalabium, and a series at the Embarcadero site that Cary Jensen later turned into a book (you can find it at Amazon - don't have a link handy).

Sign up to request clarification or add additional context in comments.

6 Comments

I already have a ton of other stuff done that uses the DBGrid and Query. If I used a TClientDataSet wouldn't I have to redo everything? I really don't want to have to do that.
No. It's a matter of changing the dataset, and reading from the query into the ClientDataSet. I converted an app that heavily used an actual database to store data entry rows to use a ClientDataSet instead in about an hour so that it would work in off-line (briefcase) mode.
For example, I have a button that lets you add new rows, despite by opening a new query and closing it after the query is executed.
In that case, you wouldn't have to do a new query to add a row; you simply do an Insert into the CDS. Please take the time to look at the link I provided for the tutorial, instead of asking additional questions here in the comments. :-)
Will you read the links I posted? I've already said that a TClientDataSet is the exact solution designed for your problem, but I'm not going to rewrite the several tutorials and documentation I've already linked to try and convince you. :-) Others have done a much better job than I could, and have a lot more space available than SO has for answers. The DB I mentioned switching to a CDS inserts thousands of rows a day into a DB containing about 2 million rows that's connected to a few dozen other tables, and it works just fine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.