Affichage des articles dont le libellé est C# datagridview. Afficher tous les articles
Affichage des articles dont le libellé est C# datagridview. Afficher tous les articles

C# Insert All DataGridView Data In MySQL

How To Add All DataGridView Values Into MySQL Database Using C#

nsert All DataGridView Records In MySQL Database Using C#,

In This C# Tutorial  We Will See How To Populate A Datagridview From Datatable And Add All Datagridview Row's Records In MySQL Database Using For Loop And Mysqlcommand with Parameters In Csharp Programming Language And Visual Studio Editor.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace Csharp_Tutorials
{
    public partial class Insert_All_DGV_Data_Into_MySQL : Form
    {
        public Insert_All_DGV_Data_Into_MySQL()
        {
            InitializeComponent();
        }

        // mysql connection
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='mydb';username=root;password=");
        
        private void Insert_All_DGV_Data_Into_MySQL_Load(object sender, EventArgs e)
        {

            DataTable table = new DataTable();



            // add columns to datatable

            table.Columns.Add("Id", typeof(int));

            table.Columns.Add("First Name", typeof(string));

            table.Columns.Add("Last Name", typeof(string));

            table.Columns.Add("Age", typeof(int));



            // add rows to datatable

            table.Rows.Add(1, "First A", "Last A", 10);

            table.Rows.Add(2, "First B", "Last B", 20);

            table.Rows.Add(3, "First C", "Last C", 30);

            table.Rows.Add(4, "First D", "Last D", 40);

            table.Rows.Add(5, "First E", "Last E", 50);

            table.Rows.Add(6, "First F", "Last F", 60);

            table.Rows.Add(7, "First G", "Last G", 70);

            table.Rows.Add(8, "First H", "Last H", 80);



            dataGridView1.DataSource = table;

        }

        // button insert
        private void buttonInsertAllData_Click(object sender, EventArgs e)
        {

            MySqlCommand command;

            connection.Open();

            for (int i = 0; i < dataGridView1.Rows.Count - 1 ; i++ )
            {
                command = new MySqlCommand("INSERT INTO dgv_data(`first_name`, `last_name`, `age`) VALUES(@ID,@fn,@ln,@AGE)", connection);

                command.Parameters.Add("@ID", MySqlDbType.Int32).Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
                command.Parameters.Add("@fn", MySqlDbType.VarChar).Value = dataGridView1.Rows[i].Cells[1].Value.ToString();
                command.Parameters.Add("@ln", MySqlDbType.VarChar).Value = dataGridView1.Rows[i].Cells[2].Value.ToString();
                command.Parameters.Add("@AGE", MySqlDbType.Int32).Value = dataGridView1.Rows[i].Cells[3].Value.ToString();

                command.ExecuteNonQuery();
             }

            connection.Close();

        }
    }
}

      

///////////////OUTPUT:





C# And MySQL Search Data Between 2 Dates

How To Search Data In MySQL Database Between Two Dates Using C#

C# And MySQL Search Records Between Two Date

In This C# Tutorial  We Will See How To Get Records Between Two Date From MySQL Database Table Using Two DateTimePicker And Display The Selected Records Into A DataGridView Rows Using Csharp Programming Language And Visual Studio Editor.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Csharp_Tutorials
{
    public partial class Search_Data_In_MySQL_Between_2_Date : Form
    {
        public Search_Data_In_MySQL_Between_2_Date()
        {
            InitializeComponent();
        }

        // mysql connection
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='mydb';username=root;password=");
        
        private void Search_Data_In_MySQL_Between_2_Date_Load(object sender, EventArgs e)
        {
            // populate datagridview from database using mysql data adapter
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM student", connection);
            DataTable table = new DataTable();

            adapter.Fill(table);

            dataGridView1.DataSource = table;
        }

        // button search
        private void BTN_Search_Click(object sender, EventArgs e)
        {

            // create a command with 2 parameters [ d1, d2 ]
            // mean the first date and second one 
            MySqlCommand command = new MySqlCommand("SELECT `id`, `first_name`, `last_name`, `birthdate`, `address` FROM `student` WHERE `birthdate` BETWEEN @d1 AND @d2", connection);

            // add values to the parameters form dateTimePickers
            command.Parameters.Add("@d1", MySqlDbType.Date).Value = dateTimePicker1.Value;
            command.Parameters.Add("@d2", MySqlDbType.Date).Value = dateTimePicker2.Value;

            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM student", connection);
            DataTable table = new DataTable();

            adapter.Fill(table);

            dataGridView1.DataSource = table;

        }
    }
}

      
///////////////OUTPUT:

Search Records Between Two Dates Using Database And C#



C# Import And Export Text File To DataGridView

How To Get And Set DataGridView Data To Txt File Text Using C#

datagridview import and export to a text file in c#

In This C# Tutorial  We Will See How To Import Records From A Text File And Display The Values Into DataGridView, and Export DataGridView Rows Data To a Txt File Using DataTable, TextWriter, StreamWriter, ReadAllLines In Csharp Programming Language And Visual Studio Editor.


PART 1


PART 2


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Csharp_Tutorials
{
    public partial class Import_Export_DGV_to_TXT_File : Form
    {
        public Import_Export_DGV_to_TXT_File()
        {
            InitializeComponent();
        }

        DataTable table1 = new DataTable();
        DataTable table2 = new DataTable();

        private void Import_Export_DGV_to_TXT_File_Load(object sender, EventArgs e)
        {

            table1.Columns.Add("ID", typeof(int));
            table1.Columns.Add("First Name", typeof(string));
            table1.Columns.Add("Last Name", typeof(string));
            table1.Columns.Add("Age", typeof(int));

            table1.Rows.Add(1, "First A", "Last A", 10);
            table1.Rows.Add(2, "First B", "Last B", 20);
            table1.Rows.Add(3, "First C", "Last C", 30);
            table1.Rows.Add(4, "First D", "Last D", 40);
            table1.Rows.Add(5, "First E", "Last E", 50);
            table1.Rows.Add(6, "First F", "Last F", 60);
            table1.Rows.Add(7, "First G", "Last G", 70);
            table1.Rows.Add(8, "First H", "Last H", 80);
            table1.Rows.Add(9, "First I", "Last I", 90);

            // populate datagridview with some data using datatable
            dataGridViewExport.DataSource = table1;

            table2.Columns.Add("ID", typeof(int));
            table2.Columns.Add("First Name", typeof(string));
            table2.Columns.Add("Last Name", typeof(string));
            table2.Columns.Add("Age", typeof(int));

            dataGridViewImport.DataSource = table2;
        }

// button export
        private void buttonExport_Click(object sender, EventArgs e)
        {
            TextWriter writer = new StreamWriter(@"C:\Users\1BestCsharp\Desktop\table2.txt");

            for (int i = 0; i < dataGridViewExport.Rows.Count - 1; i++) // rows
            {

                for (int j = 0; j < dataGridViewExport.Columns.Count; j++) // columns
                {
                    if(j == dataGridViewExport.Columns.Count - 1 ) // if last column
                    {
                        writer.Write("\t" + dataGridViewExport.Rows[i].Cells[j].Value.ToString());
                    }

                    else
                    writer.Write("\t" + dataGridViewExport.Rows[i].Cells[j].Value.ToString() + "\t" + "|");

                }

                writer.WriteLine("");

            }

            writer.Close();
            MessageBox.Show("Data Exported");
        }

// button import
        private void buttonImport_Click(object sender, EventArgs e)
        {
            string[] lines = File.ReadAllLines(@"C:\Users\1BestCsharp\Desktop\table2.txt");
            string[] values;


            for (int i = 0; i < lines.Length; i++)
            {
                values = lines[i].ToString().Split('|');
                string[] row = new string[values.Length];

                for (int j = 0; j < values.Length; j++)
                {
                    row[j] = values[j].Trim();
                }
                table2.Rows.Add(row);
            }
        }

     
    }
}



      
///////////////OUTPUT:

import and export txt file text to datagridview using c#



C# Import Text File Data To DataGridview

How To Populate DataGridview From a Text File In C#

display txt file text to a datagridview using c#

In This C# Tutorial  We Will See How To Import Records From A Text File And Display The Values Into DataGridView Rows On Button Click Event Using Csharp Programming Language And Visual Studio Editor.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Csharp_Tutorials
{
    public partial class TXT_TO_DGV : Form
    {
        public TXT_TO_DGV()
        {
            InitializeComponent();
        }
        DataTable table = new DataTable();
        private void TXT_TO_DGV_Load(object sender, EventArgs e)
        {
        
            // add columns to datatable
            table.Columns.Add("Id", typeof(int));
            table.Columns.Add("First Name", typeof(string));
            table.Columns.Add("Last Name", typeof(string));
            table.Columns.Add("Age", typeof(int));

            dataGridView1.DataSource = table;

        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
           // get lines from the text file
            string[] lines = File.ReadAllLines(@"C:\Users\1BestCsharp\Desktop\table.txt");
            string[] values;


            for(int i = 0; i < lines.Length; i++)
            {
                values = lines[i].ToString().Split('|');
                string[] row = new string[values.Length];

                for (int j = 0; j < values.Length; j++)
                {
                    row[j] = values[j].Trim();
                }
                table.Rows.Add(row);
            }

        }
    }
}


      
///////////////OUTPUT:

Import Text File Text To DataGridview Using C#




Add Row To Datagridview From Another Form In C#

How to Add a New Row To Datagridview From Another Form Using C#

Add Row To Datagridview From Another Form Using C#


In This C# Tutorial We Will See How To Insert A New Row To a Datagridview With Data From TextBoxes In Another Form Using C# Windows Form and Visual Studio Editor .

WATCH THIS C# TUTORIAL


Project Source Code:


// ------ First Create a Form  Where You Put The Datagridview 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Csharp_Tutorials.rst
{
    public partial class FRM_GRIDVIEW : Form
    {
        public FRM_GRIDVIEW()
        {
            InitializeComponent();
        }

// button to open the form where the textboxes are in
        private void button_NewForm_Click(object sender, EventArgs e)
        {
            FormINFO finfo = new FormINFO(this);
            finfo.Show();
        }
    }
}


Datagridview Form



// ------ Create a Form With TextBoxes

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Csharp_Tutorials.rst
{
    public partial class FormINFO : Form
    {
        FRM_GRIDVIEW fgrid;
        public FormINFO(FRM_GRIDVIEW fg)
        {
            InitializeComponent();
            this.fgrid = fg;
        }

// button to add the row with the data to the datagridview
        private void button_Add_Click(object sender, EventArgs e)
        {
            fgrid.dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text);
        }

    }
}


TextBoxes Form



 - Watch The Video Tutorial To See The Scond Method ( making the datagrdivew static ) 



OUTPUT:


C# - Insert Row To Datagridview From Another Form