Dapper CRUD Win Forms - Part Two

The source code is in part one of the articles,

Fill the variables

 
In the program.cs the following code must be created. The variable path is assigned the value of the path where the conn.xml file is located and the variable Utilities.Globals.stringConn is assigned the value string connection of the function xml_conn,
    1. using DapperRepoWinForm.Forms;  
    2. namespace DapperRepoWinForm {  
    3.     static class Program {  
    4.         /// <summary>    
    5.         /// The main entry point for the application.    
    6.         /// </summary>    
    7.         [STAThread]  
    8.         static void Main() {  
    9.   
    10.             showForm();  
    11.         }  
    12.   
    13.         public static void showForm() {  
    14.             try {  
    15.                 //Remenber you must have this file--> c:\conn.xml    
    16.                 string path = Utilities.Globals.path;  
    17.                 Utilities.Globals.stringConn = ConnectionDB.xml_conn(path);  
    18.                 Application.EnableVisualStyles();  
    19.                 Application.SetCompatibleTextRenderingDefault(false);  
    20.                 Form1 frm = new Form1();  
    21.                 frm.Show();  
    22.                 Application.Run();  
    23.             } catch (Exception e) {  
    24.                 MessageBox.Show(e.Message);  
    25.             }  
    26.             // Do some more work here    
    27.         }  
    28.     }  

      Working with Forms and Controls

       
      In the form1 form, we are going to put some controls. The name of the controls to use are shown in the following image,
       
       
      Note
       
      In the datagridview2, in each column, the Name value must be equal to the name of the datatable field that fills it,
       
       
      For this example the values ​​are,
       
      ID USER = id
      USERNAME = name
      SHORT ADDRESS = address
      USER STATUS = status
       
      Adding some code
      • First we create an _user variable of type ClassObjects.users (); Which we will use in the field of form1
      • Second we create a _metod variable of type Bll.users (); Which we will use in the scope of form1 and will
      • serve to call the "methods" class BillUsers
        1. using System.Windows.Forms;  
        2. using System.Collections.Generic;  
        3. using System.Data;  
        4.   
        5. public partial class Form1: Form {  
        6.     ClassObjects.users _user = new ClassObjects.users();  
        7.     Bll.users _metod = new Bll.users();  
        8.     public Form1() {  
        9.         InitializeComponent();  
        10.     }  
        11.   
        12.     private void Form1_Load(object sender, System.EventArgs e) {  
        13.     }  
           A function to clean the fields, 
            1. private void cleanform() {  
            2.     foreach(Control c in this.Controls) {  
            3.         if (c is TextBox) {  
            4.             (c as TextBox).Text = string.Empty;  
            5.         }  
            6.     }  

              Code for toolstrip´s buttoms 

               
              Button Save: Call function to clean form´s fields, 
              1. private void btnNew_Click(object sender, System.EventArgs e) {  
              2.     cleanform();  

              Button Save

               
              Assign values ​​to the attributes of the _user class, then create a variable msg and this variable receives the value of the result of the process _metod.insertUpdate,
                1. private void btnSave_Click(object sender, System.EventArgs e) {  
                2.     _user.id = textBox1.Text;  
                3.     _user.name = textBox2.Text;  
                4.     _user.address = textBox3.Text;  
                5.     _user.status = textBox4.Text;  
                6.     string msg = _metod.insertUpdate(_user);  
                7.     if (msg == "0") {  
                8.         MessageBox.Show("Record added");  
                9.         cleanform();  
                10.     } else {  
                11.         MessageBox.Show(msg);  
                12.     }  
                13.   
                  Button clean: we will only clean the textboxs form,
                    1. private void btnClear_Click(object sender, System.EventArgs e) {  
                    2.     cleanform();  

                      Delete button

                       
                      This code deletes the record whose Id matches the value of the TextBox1 control,
                      1. private void btnDelete_Click(object sender, System.EventArgs e) {  
                      2.     _user.id = textBox1.Text;  
                      3.     string msg = _metod.delete(_user);  
                      4.     if (msg == "0") {  
                      5.         MessageBox.Show("record deleted");  
                      6.         cleanform();  
                      7.     } else {  
                      8.         MessageBox.Show(msg);  
                      9.     }  

                      Search button

                       
                      This button executes a record search based on the ID field written to the TextBox1 control
                        1. private void btnFind_Click(object sender, System.EventArgs e) {  
                        2.     if (textBox1.Text != "") {  
                        3.         var items = _metod.findById(textBox1.Text);  
                        4.         if (items != null) {  
                        5.             textBox2.Text = items.name;  
                        6.             textBox3.Text = items.address;  
                        7.             textBox4.Text = items.status;  
                        8.         } else {  
                        9.             MessageBox.Show("Record not found");  
                        10.         }  
                        11.     }  

                          Populate Button

                           
                          This button returns a list of class _user, and fills the grid. For this exercise columns were created in the grid, then the properties of the _user class are traversed and the Name value is assigned to the property of the dataGridView2.Columns [n] .DataPropertyName,
                            1. private void btnPopulate_Click(object sender, System.EventArgs e) {  
                            2.     List < ClassObjects.users > lista = _metod.allRecords(_user);  
                            3.     dataGridView1.DataSource = lista;  
                            4.     int i = 0;  
                            5.     string xx = "";  
                            6.     dataGridView2.AutoGenerateColumns = false;  
                            7.     foreach(var prop in _user.GetType().GetProperties()) {  
                            8.         dataGridView2.Columns[i].DataPropertyName = prop.Name;  
                            9.         i = i + 1;  
                            10.     }  
                            11.     dataGridView2.DataSource = lista;  

                              Populate Dynamics button

                               
                              This button returns a dynamic list so that you can fill the grid with the FG.ConvertToDataTable ()
                                1. private void btnPopulateDyn_Click(object sender, System.EventArgs e) {  
                                2.     Utilities.Funciones FG = new Utilities.Funciones();  
                                3.     var items = _metod.dynamicsList();  
                                4.     DataTable dt = new DataTable();  
                                5.     dt = FG.ConvertToDataTable(items);  
                                6.     dataGridView1.DataSource = dt;  
                                7.     dataGridView2.AutoGenerateColumns = false;  
                                8.     for (int i = 0; i < dt.Columns.Count - 1; i++) {  
                                9.         dataGridView2.Columns[i].DataPropertyName = dt.Columns[i].ColumnName;  
                                10.     }  
                                11.     dataGridView2.DataSource = dt;  
                                  I hope this small example will help your projects. I'm always attentive to your comments.


                                  Similar Articles