How To Insert DataGridView Multiple Row Values in Window Application using C#


Insert DataGridView Multiple Row Values  Using Window Application



DataGridView multiple Row  Values Insert into DataBase  using Window Application C#



DEMO





C# Coding


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.Data.SqlClient;


namespace QueryWindows
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\drizzles\documents\visual studio 2012\Projects\QueryWindows\QueryWindows\Database1.mdf;Integrated Security=True");

            con.Open();

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

                string amounts = dataGridView1.Rows[i].Cells[0].Value.ToString();

                SqlCommand cmd = new SqlCommand("insert into register(amount)values('" + amounts + "')", con);

                cmd.ExecuteNonQuery();

                MessageBox.Show("Data Registered");

            }

        }
    }
}





First Create - New Window Form 








Next - Add DataGridView From Data  Toolbox - Right Click - Select - Add Column - Add New Column










Next - Select - Edit Column - Change the Name - HeaderText - OK











Next - Add Button From Toolbox - Change Text Name













Next - Create DataBase Table Name - amount











Next - Create SqlConnection - Select SqlConnection  Control From  Toolbox -  Go to Property - Connection String - Select - Database Name Get Path Name 










Next - For Loop DataGridView Row Values Get and  Insert










Next - Run the Program[F5] - Enter GridView Row Values Manually









Next - Insert the DatGridView  Values to DataBase








OUTPUT


Show the Inserted Row Values







0 comments:

Post a Comment