Sunday, July 4, 2010

8 Month Old With Chesty Cough

SDL,

We have seen how to display data in a SQLite database from C #, both from console as using Windows Forms and basic components such as TextBox and ListView.

If we simply a "grid" similar to those of spreadsheets, which can modify and add data, there is a simple way to get it: from the visual editor added a DataGridView to our form and connect to our table SQLiteDataAdapter using a DataSet and a DataTable.

appearance would this

And the source would be essentially the same as above, but the function responsible for updating the list of cities would look like:

1:

private void ActualizarListaCiudades (

) 2: {

3: connection = 4 :

new SQLiteConnection 5: (

"Data Source = personal.sqlite, Version = 3; New = False; Compress = True;" )

;

6:

connection. Open

(

)

;

7:
8:

/ / launch the consultation and prepare to read data structure
9:

string query =

"select * from city "
;

10:


11:
   / / Data Adapter, DataSet and Table   12:    SQLiteDataAdapter  db = new      SQLiteDataAdapter   (   
consultation, connection )
;

13: DataSet ds = new DataSet
( ) ; 14: ds
. Reset ( ) ; 15: DataTable dt

= new DataTable
(
)
; 16: db
.

Fill (ds )
;
17: 18: / / Assign the DataTable the first table (cities) 19: / / and show it in the DataGridView 20:
dt = ds
. Tables [ 0 ] ;
21: dataGridView1.DataSource dt = ; 22: 23: / / And we can close the connection 24:

connection. Close ( ) ; 25:
}