Friday, June 25, 2010

What Does Fungus Look Like

DataGridView C # and SQLite with C # on Windows Forms

After seeing how to create a console application in C # to create a database with SQLite, save data and read, we'll see how to do something similar in an application "window" using Windows Forms.

components is done using "normal" rather than the defaults that allow access to databases. We will have a main window that displays the current data in a ListView, and includes a button to add new data:

button "Add" show a second form which will ask the data:

We can do this by following these steps (in Visual C # 2008): Create a new project, which is a "Windows Forms Application" In the main window, add a ListView and a button. Customize ListView using the properties tab, so you have two columns (one for code and one for the name), and as a display mode ("View") have the "details" ("Details" .) Creating a second window from the "Project" in the "Add Windows Form." Add to that window two TextBox for the text that the user entered, along with explanatory text for the Label and a button to confirm. Add the DLL file to your project references (in the window of "Solution Explorer" by right clicking on "References" and selecting the file System.Data.SQLite.DLL from the "Browse" tab.) complete source code of the project. As the code, the two key parts are the part that reads the data to populate the ListView and releasing the auxiliary window, making it what the user type and then enter that data.

The filling in the ListView (which is called the end of the constructor, and after the introduction of new data) could be:

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:
  • lstCiudades
  • . Items
. Clear

(

)


;


  9:    10:    / / launch the consultation and prepare to read data structure   11: query string       = "select * from city"   ; 
12: SQLiteCommand cmd = new

SQLiteCommand (
consultation, connection
) ; 13: SQLiteDataReader data Cmd = .

ExecuteReader ( ) ; 14: / / read data repeatedly 15:

while ( data. Read ( ) )

16: {

17: string code = Convert
. ToString (
data [0 ] ) ; 18: string name = Convert .

ToString ( data [1 ] ) ;
19:
/ / And show
20: ListViewItem item = new ListViewItem ( code) ; 21:

item. SubItems . Add
( name) ; 22: lstCiudades . Items . Add (item )

; 23: } 24: connection. Close ( ) ;
25: }

And adding other data would be like: 1: private void InsertarDatos
( string code, string name ) 2: { 3:
4:
insertion string; / / Order of insertion, in SQL 5: cmd SQLiteCommand ; / / Command SQLite 6:
7: connection =
8: new SQLiteConnection 9: ( "Data Source = personal.sqlite, Version = 3, New = False; Compress = True; " )

; 10:
connection. Open

(


)
   ;     11:    12:        try 13: {     
14: insertion = "INSERT INTO city"

+
15: "VALUES ('" + + code
"','" + name +
"');" ;

16:
cmd = new

SQLiteCommand
( insertion,
connection ) ; 17: cmd . ExecuteNonQuery (

) ; 18: } 19:

catch (Exception e
) 20:

{21: MessageBox . Show
( 22: "Unable to insert. Possibly a code is repeated" , 23: "Notice" )

; 24: } 25: connection. Close ( )
; 26: } So the complete action on the button "Add" would be something like

1:
private void btAnadir_Click ( object sender, EventArgs e )
2: {
3: ventanaAnadir . Clean (

) ;
4: ventanaAnadir . ShowDialog ( )
;
5: InsertarDatos ( ventanaAnadir . GetCodigo ( )

, ventanaAnadir .
getName (



)
   )    ;      6:  ActualizarListaCiudades   (   )    
; 7:

} And here you can download the project, with the two sources, the DLL and executable files: csharp_sqlite2.zip

0 comments:

Post a Comment