Hi need help i setting up both Adding and Updating datagrid cells in the Save_Click event.
Right i used seperate click events for Add_Click and Updating_Click but i want to both of them in the Save_Click event rather than seperately. Thanks in advance Here is my code

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

namespace WindowsFormsApplication1
{
    public partial class Reservations_Form : Form
    {
        public Reservations_Form()
        {
            InitializeComponent();
        }

        DataSet ds = new DataSet();
        SqlConnection cs = new SqlConnection(@"Data Source=SAIPAYILAVARAPU\newproject2;Initial Catalog=Reservation;Integrated Security=True");
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand();
        BindingSource tblCustomers = new BindingSource();
        DataView dview = new DataView();
       // CurrencyManager cmgr;
        // DataRowView drv;

        private void Form1_Load(object sender, EventArgs e)
        {


                da.SelectCommand = new SqlCommand("SELECT *FROM Customers", cs);
                da.Fill(ds);
                dg.DataSource = ds.Tables[0];
                tblCustomers.DataSource = ds.Tables[0];
                //tbxFirstName.DataBindings.Add(new Binding("Text", tblReservations, "First_Name"));
                //tbxLastName.DataBindings.Add(new Binding("Text", tblReservations, "Last_Name"));
                //cmgr = (CurrencyManager)dg.BindingContext[ds.Tables[0]];
                //cmgr.Position = 0;
                //drv = (DataRowView)cmgr.Current;



        }



        private void btnSave_Click(object sender, EventArgs e)
        {

            string col1 = dg[0, dg.CurrentCell.RowIndex].Value.ToString();
            string col2 = dg[1, dg.CurrentCell.RowIndex].Value.ToString();
            string col3 = dg[2, dg.CurrentCell.RowIndex].Value.ToString();
            string col4 = dg[3, dg.CurrentCell.RowIndex].Value.ToString();
            string col5 = dg[4, dg.CurrentCell.RowIndex].Value.ToString();
            string col6 = dg[5, dg.CurrentCell.RowIndex].Value.ToString();
            string col7 = dg[6, dg.CurrentCell.RowIndex].Value.ToString();
            string col8 = dg[7, dg.CurrentCell.RowIndex].Value.ToString();
            string col9 = dg[8, dg.CurrentCell.RowIndex].Value.ToString();


            string insert_sql = "INSERT INTO Customers(First_Name, Last_Name, Type_Of_Trip, Leaving_From, Going_To, Departing_Date,Returning_Date,Departing_Time, Returning_Time) VALUES ('" + col1 + "','" + col2 + "','" + col3 + "','" + col4 + "','" + col5 + "','" + col6 + "','" + col7 + "','" + col8 + "','" + col9 + "')";

               // this.getcom(insert_sql);
            DialogResult button = MessageBox.Show ("Are you sure you want to save this data?","Save Data?", MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
            if (button == DialogResult.Yes)
            {
                this.getcom(insert_sql);
                MessageBox.Show("Record Added");
            }

        }

        public SqlConnection GetSqlConnection()  //connection function
        {
            string str_sqlcon = "Data Source=SAIPAYILAVARAPU\\newproject2;Initial Catalog=Reservation;Integrated Security=True";
            SqlConnection mycon = new SqlConnection(str_sqlcon);
            mycon.Open();
            return mycon;
        }

        public void getcom(string sqlstr)  //function for adding rows
        {
            SqlConnection sqlcon = this.GetSqlConnection();  // Watch out same string type as GetSQLConnection function
            SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcom.ExecuteNonQuery();
            sqlcom.Dispose();
            sqlcon.Close();
            sqlcon.Dispose();
        }


        private void btnInsert_Click(object sender, EventArgs e)
        {

            string col1 = dg[0, dg.CurrentCell.RowIndex].Value.ToString();
            string col2 = dg[1, dg.CurrentCell.RowIndex].Value.ToString();
            string col3 = dg[2, dg.CurrentCell.RowIndex].Value.ToString();
            string col4 = dg[3, dg.CurrentCell.RowIndex].Value.ToString();
            string col5 = dg[4, dg.CurrentCell.RowIndex].Value.ToString();
            string col6 = dg[5, dg.CurrentCell.RowIndex].Value.ToString();
            string col7 = dg[6, dg.CurrentCell.RowIndex].Value.ToString();
            string col8 = dg[7, dg.CurrentCell.RowIndex].Value.ToString();
            string col9 = dg[8, dg.CurrentCell.RowIndex].Value.ToString();


            string insert_sql = "INSERT INTO Customers(First_Name, Last_Name, Type_Of_Trip, Leaving_From, Going_To, Departing_Date,Returning_Date,Departing_Time, Returning_Time) VALUES ('" + col1 + "','" + col2 + "','" + col3 + "','" + col4 + "','" + col5 + "','" + col6 + "','" + col7 + "','" + col8 + "','" + col9 + "')";
            this.getcom(insert_sql);

        }


        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //"UPDATE Customers SET Last_Name = col2, Type_Of_Trip = col3, Leaving_From = col4, Going_To = col5,Departing_Date = col6, Returning_Date = col7, Departing_Time = col8, Returning_Time = col9 WHERE First_Name = col1" 

            string col1 = dg[0, dg.CurrentCell.RowIndex].Value.ToString();
            string col2 = dg[1, dg.CurrentCell.RowIndex].Value.ToString();
            string col3 = dg[2, dg.CurrentCell.RowIndex].Value.ToString();
            string col4 = dg[3, dg.CurrentCell.RowIndex].Value.ToString();
            string col5 = dg[4, dg.CurrentCell.RowIndex].Value.ToString();
            string col6 = dg[5, dg.CurrentCell.RowIndex].Value.ToString();
            string col7 = dg[6, dg.CurrentCell.RowIndex].Value.ToString();
            string col8 = dg[7, dg.CurrentCell.RowIndex].Value.ToString();
            string col9 = dg[8, dg.CurrentCell.RowIndex].Value.ToString();

            string sql_update = "UPDATE Customers SET Last_Name='" + col2  + "',Type_Of_Trip = '" + col3 + "',Leaving_From = '" + col4 + "', Going_To = '" + col5 + "', Departing_Date = '" + col6 + "', Returning_Date = '" + col7 + "', Departing_Time = '" + col8 + "', Returning_Time = '"+col9 +"' WHERE First_Name = '" + col1 +"'";
            DialogResult button = MessageBox.Show("Are you sure you want to Update the Data?", "Update Data?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (button == DialogResult.Yes)
            {
                this.getcom(sql_update);
                MessageBox.Show("Record Updated");
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult button = MessageBox.Show("Do you want to Cancel the Entering the Data?", "Cancel?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (button == DialogResult.Yes)
            {
                tbxFirstName.Focus();
            }
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {

                DataSet ds = new DataSet();
                da.SelectCommand = new SqlCommand("SELECT *FROM Customers", cs);
                da.Fill(ds);
                dg.DataSource = ds.Tables[0];

                tbxFirstName.DataBindings.Add( new Binding ("Text", tblCustomers, "First_Name"));
                tbxFirstName.DataBindings.Clear();
                tbxLastName.DataBindings.Add( new Binding ("Text", tblCustomers, "Last_Name"));
                tbxLastName.DataBindings.Clear();

            //tblReservations.DataSource = ds.Tables[0];
            //tbxFirstName.DataBindings.Add(new Binding("Text", tblReservations, "First_Name"));
            //tbxLastName.DataBindings.Add(new Binding("Text", tblReservations, "Last_Name"));

        }

        private void dg_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }



        private void btnSearch_Click_1(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter("SELECT *FROM Customers WHERE First_Name =  '" + tbxFirstName.Text + "'", cs);
            da.Fill(dt);
            dg.DataSource = dt;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string col1 = dg[0, dg.CurrentCell.RowIndex].Value.ToString();
            string col2 = dg[1, dg.CurrentCell.RowIndex].Value.ToString();
            string col3 = dg[2, dg.CurrentCell.RowIndex].Value.ToString();
            string col4 = dg[3, dg.CurrentCell.RowIndex].Value.ToString();
            string col5 = dg[4, dg.CurrentCell.RowIndex].Value.ToString();
            string col6 = dg[5, dg.CurrentCell.RowIndex].Value.ToString();
            string col7 = dg[6, dg.CurrentCell.RowIndex].Value.ToString();
            string col8 = dg[7, dg.CurrentCell.RowIndex].Value.ToString();
            string col9 = dg[8, dg.CurrentCell.RowIndex].Value.ToString();

            string sql_update = "DELETE FROM Customers WHERE First_Name = '" + col1 + "'";
            DialogResult button = MessageBox.Show("Are you sure you want to Delete the Record?", "Update Record?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (button == DialogResult.Yes)
            {
                this.getcom(sql_update);
                MessageBox.Show("Record Deleted");
            }
        }

        private void tbxFirstName_TextChanged(object sender, EventArgs e)
        {
            if (tbxFirstName.Text != null)
            {
                btnSearch.Enabled = true;

            }
            else
            {
                btnSearch.Enabled = false;
            }
        }

        private void dg_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            btnInsert.Enabled = true;
            btnDelete.Enabled = true;
            btnUpdate.Enabled = true;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
        }



    }
}

You can pass the gridview1 object as well as rowindex from form1 to form2 while clicking on the button. then after finishing the work in form2, update the gridview1 from form1 for that row, with the help of the gridview1 object and rowindex IN FORM2 itself.

Edited 4 Years Ago by pritaeas: Removed self promotion link.

Hi,
It looks like you are making a lot of hard work. I guess if you are setting datasource for you grid:

this.grid.DataSource = table

all your changes in the grid is coming to your source. So you don't need to read every time any cells from the grid and put its to a SQL statment. This case is simple. Just use any Sql - adapter for your source table nd update that source.

Regards

This article has been dead for over six months. Start a new discussion instead.