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?

user1970794
  • 223
  • 3
  • 14

1 Answers1

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).

Ken White
  • 117,855
  • 13
  • 197
  • 405
  • 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. – user1970794 Jan 24 '13 at 01:19
  • 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. – Ken White Jan 24 '13 at 01:21
  • 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. – user1970794 Jan 24 '13 at 01:23
  • 1
    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. :-) – Ken White Jan 24 '13 at 01:24
  • Also it seems like if you have a massive database then TClientDataSet would be a bad idea. I'm talking about how you would do it with a large multitable multiuser database. – user1970794 Jan 24 '13 at 01:26
  • 4
    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. – Ken White Jan 24 '13 at 01:35