none
Writing from datagridview to database RRS feed

  • Question

  • I have this tab control with 4 tabs with a datagridview on each page. The datagridviews have 10 rows by 5 columns. I want to be able to rewrite the data in the datagridviews as it is for a spelling bee and the it will be the spellings that are rewritten. The save button saves it to the dataset but does not save to the database. How do I save it to the database. I am quite obviously a complete novice and it doesnt come easilly to me.

    Tuesday, April 8, 2014 12:19 PM

Answers

  • Hello,

    A way to this is:

    1. Fill dataTable and use it as a biningsource to datagridview

    2. Do changes in datagridview...

    3. Press the save button.

    An example is like follow:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Data.SqlClient;
    
    using System.Drawing;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Windows.Forms;
    
    
    namespace WinForm03
    
    {
    
        public partial class Form1 : Form
    
        {
    
            private DataTable dt = new DataTable();
    
    
            private SqlDataAdapter da = new SqlDataAdapter();
    
    
            private SqlConnection connection;
    
    
            string connectionString = @"Data Source=(localdb)\Projects;Initial Catalog=DFDB;Integrated Security=True";
    
    
            public Form1()
    
            {
    
                InitializeComponent();
    
    
                this.dataGridView1.DataSource = this.bindingSource1;
    
            }
    
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                dt.Clear();
    
    
                connection = new SqlConnection(connectionString);
    
                try
    
                {
    
                    connection.Open();
    
    
                    using (SqlCommand cmd = new SqlCommand(@"select * from [Order]", connection))
    
                    {
    
                        da = new SqlDataAdapter();
    
    
                        da.SelectCommand = cmd;
    
    
                        SqlCommandBuilder builder = new SqlCommandBuilder(da);
    
    
                        da.Fill(dt);
    
    
                        this.bindingSource1.DataSource = dt;
    
                    }
    
                }
    
                catch (Exception ex)
    
                {
    
                    Console.WriteLine(ex.Message);
    
                }
    
                finally
    
                {
    
                    connection.Close();
    
                }
    
    
            }
    
    
            private void button2_Click(object sender, EventArgs e)
    
            {
    
                try
    
                {
    
                    if (connection.State == ConnectionState.Closed)
    
                    {
    
                        connection.Open();
    
                    }
    
    
                    da.Update((DataTable)this.bindingSource1.DataSource);
    
                }
    
                catch (Exception)
    
                {
    
                    connection.Close();
    
                }
    
            }
    
        }
    
    }
    

    The result:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 9, 2014 3:20 AM
    Moderator