Save DataGridView Rows into Database
In this article, I will show you how to save DataGridView Rows into Database, let’s follow code below
Step 1:
Create Project And Add Reference To C# WinForms Project. (visit the link to the first tutorial on how to add reference to our C# project)
How to connect MySQL Database to C# Tutorial and Source code
Step 2:
Back to windows forms application and design form like this below
Form Components:
- Dtuser
- Btnsave
Step 3:
Add Columns in DataGridView Using Datatable see code below, and add some data.
using MySql.Data.MySqlClient; 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 savedatagridviewrowsintodatabase { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable t = new DataTable(); t.Columns.Add("NAME", typeof(string)); t.Columns.Add("NUMBER", typeof(string)); t.Columns.Add("ADDRESS", typeof(string)); t.Columns.Add("BIRTHDAY", typeof(string)); dtgridview.DataSource = t; t.Rows.Add("mark", 6435654,"albay", "2020-07-01"); t.Rows.Add("gdfg", 6435543654, "philippines", "2020-07-02"); t.Rows.Add("madfgdfgrk", 643544654, "bohol", "2020-07-03"); } } }
Code Explanation:
This code explain how to add columns in datagridview rows add some data.
Step 4:
Code for Saving data from datagridview rows to database btnsave click events:
using MySql.Data.MySqlClient; 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 savedatagridviewrowsintodatabase { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnsave_Click(object sender, EventArgs e) { for (int i = 0; i < dtgridview.Rows.Count; i++) { MySqlConnection con = new MySqlConnection("datasource= localhost; database=sampledb;port=3306; username = root; password= db1234"); con.Open(); MySqlCommand cmd = new MySqlCommand("insert into information(name,number,address,birthday) values(@name,@number,@address,@birthday)", con); cmd.Parameters.AddWithValue("@name", dtgridview.Rows[i].Cells[0].Value); cmd.Parameters.AddWithValue("@number", dtgridview.Rows[i].Cells[1].Value); cmd.Parameters.AddWithValue("@address", dtgridview.Rows[i].Cells[2].Value); cmd.Parameters.AddWithValue("@birthday", dtgridview.Rows[i].Cells[3].Value); cmd.ExecuteNonQuery(); con.Close(); } MessageBox.Show("Successfully Added", "VINSMOKE MJ", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
Code Explanations:
This code explain how to save data from datagridview rows to database continuously to the end of the datagridview rows.
Output:
Save DataGridView Rows into Database Free Download Sourcecode
Mark Jaylo
YTC: https://www.youtube.com/c/MarkTheProgrammer
You may visit our facebook page for more information, inquiries and comments.
Hire our team to do the project.