Affichage des articles dont le libellé est dataGridView. Afficher tous les articles
Affichage des articles dont le libellé est dataGridView. Afficher tous les articles

VB.Net Transfer TreeView Nodes To DataGridView

How To Set TreeView Nodes Values Into DataGridView Rows Using Visual Basic .Net

Transfer TreeView Nodes To DataGridView Using VB.Net

In this VB.NET Tutorial we will see How To Get TreeView Nodes Data And Set It Into DataGridView Rows Using For Loop On Button Click Event In Visual Basic.Net  Programming Language And Visual  Studio Editor.




Project Source Code:


Public Class TreeView_To_Datagridview

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' go through all nodes
        For i As Integer = 0 To TreeView1.Nodes.Count - 1 Step +1

            ' create a new node
            Dim node As New TreeNode()

            ' set the created node equal to the current one
            node = TreeView1.Nodes(i)
           
            ' create a row
            Dim row(node.Nodes.Count) As Object

            ' add values from node to the row
            For j As Integer = 0 To node.Nodes.Count - 1 Step +1

                row(j) = node.Nodes(j).Text

            Next j
            
            ' add row to the datagridview
            DataGridView1.Rows.Add(row)

        Next i

    End Sub
End Class


OutPut:

TreeView To DataGridView In VB.Net




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#



VB.Net Import And Export Text File To DataGridView

How To Get And Set DataGridView Data To Txt File Text Using Visual Basic .Net

datagridview import and export to a text file in vb.net



In this VB.NET 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 Visual Basic.Net   Programming Language And Visual  Studio Editor.




Project Source Code:


Imports System.IO

Public Class TXT_IMPORT_EXPORT_DGV

    Dim table1 As New DataTable()
    Dim table2 As New DataTable()

    Private Sub TXT_IMPORT_EXPORT_DGV_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' add columns to your datatable
        ' with the name of the columns and their type

        table1.Columns.Add("Id", Type.GetType("System.Int32"))
        table1.Columns.Add("First Name", Type.GetType("System.String"))
        table1.Columns.Add("Last Name", Type.GetType("System.String"))
        table1.Columns.Add("Age", Type.GetType("System.Int32"))

        ' add rows to datatable with some data
        table1.Rows.Add(1, "AAAA", "BBBB", 21)
        table1.Rows.Add(2, "CCCC", "DDDD", 33)
        table1.Rows.Add(3, "EEEE", "FFFF", 53)
        table1.Rows.Add(4, "GGGG", "HHHH", 19)
        table1.Rows.Add(5, "MMMM", "NNNN", 36)
        table1.Rows.Add(6, "RRRR", "SSSS", 63)

        ' now set the datagridview datasource equals to your datatable name
        DataGridView1.DataSource = table1


        table2.Columns.Add("Id", Type.GetType("System.Int32"))
        table2.Columns.Add("First Name", Type.GetType("System.String"))
        table2.Columns.Add("Last Name", Type.GetType("System.String"))
        table2.Columns.Add("Age", Type.GetType("System.Int32"))


        DataGridView2.DataSource = table2

    End Sub

' button export
    Private Sub ButtonExport_Click(sender As Object, e As EventArgs) Handles ButtonExport.Click

        Dim writer As New StreamWriter("C:\Users\1BestCsharp\Desktop\Table2.txt")

        For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1

            For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1

                ' if last column
                If j = DataGridView1.Columns.Count - 1 Then
                    writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString())
                Else
                    writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString() & vbTab & "|")
                End If


            Next j

            writer.WriteLine("")

        Next i

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

    End Sub

    ' button import
    Private Sub ButtonImport_Click(sender As Object, e As EventArgs) Handles ButtonImport.Click

        Dim lines() As String
        Dim vals() As String

        lines = File.ReadAllLines("C:\Users\1BestCsharp\Desktop\table2.txt")

        For i As Integer = 0 To lines.Length - 1 Step +1

            vals = lines(i).ToString().Split("|")
            Dim row(vals.Length - 1) As String

            For j As Integer = 0 To vals.Length - 1 Step +1

                row(j) = vals(j).Trim()

            Next j

            table2.Rows.Add(row)

        Next i

    End Sub
End Class


OutPut:

import and export txt file text to datagridview using vb.net





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#