Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi !

I can Work on GridView (Insert/Edit/Update/Delete)in Web Based with ASP.NET,C#.
But I do not know similar solution in Windows Based Application
How to Work on DataGridView (Insert/Edit/Udate/Delete) in Windows Based


Please suggest.
Posted
Updated 8-Apr-13 22:30pm
v2
Comments
OriginalGriff 9-Apr-13 4:26am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

its depends on many things like which database you are using n all that
but suppose if em using ms access the code will be like
C#
Try this...



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.OleDb;
namespace Insert_Update_Delete_DataGridView
{
    public partial class Form1 : Form
    {
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        OleDbCommand com;
        OleDbDataAdapter oledbda;
        string str;
        DataSet ds;
   
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bindgrid();
        }
        private void bindgrid()
        {
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from employee";
            com = new OleDbCommand(str, con);
            oledbda = new OleDbDataAdapter(com);
            ds = new DataSet();
            oledbda.Fill(ds, "employee");
            dataGridView1.DataMember="employee";
            dataGridView1.DataSource = ds;
            con.Close(); 
        }
        private void Clear()
        {
            txt_sno.Text = "";
            txt_name.Text = "";
            txt_address.Text = "";
            txt_age.Text = "";
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            txt_sno.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            txt_name.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
            txt_address.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            txt_age.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "Update employee set sno=@sno,name=@name,address=@address,age=@age Where sno=@sno";
            com = new OleDbCommand(str, con);
            com.Parameters.AddWithValue("@sno", txt_sno.Text);
            com.Parameters.AddWithValue("@name", txt_name.Text);
            com.Parameters.AddWithValue("@address", txt_address.Text);
            com.Parameters.AddWithValue("@age", txt_age.Text);
            com.ExecuteNonQuery();
            MessageBox.Show("Record Successfully Updated");
            con.Close();
            bindgrid();
            Clear();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(ConnectionString);
            com = new OleDbCommand();
            if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
            {
                com.CommandText = "DELETE FROM employee WHERE ID=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
                con.Open();
                com.Connection = con;
                com.ExecuteNonQuery();
                con.Close();
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                MessageBox.Show("Record Successfully Deleted");
            }
            bindgrid();
        }

        private void btn_insert_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "Insert into employee(sno,name,address,age) Values (@sno,@name,@address,@age)";
            com = new OleDbCommand(str, con);
            com.Parameters.AddWithValue("@sno", txt_sno.Text);
            com.Parameters.AddWithValue("@name", txt_name.Text);
            com.Parameters.AddWithValue("@address", txt_address.Text);
            com.Parameters.AddWithValue("@age", txt_age.Text);
            com.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Record Successfully Inserted");
            bindgrid();
            Clear();
        }
    }
}
 
Share this answer
 
Comments
LebneizTech 10-Apr-13 5:18am    
Thank You Sir,
It is working fine.
Now I require to Insert without Text Box, means a new row should create in Data Grid on Run Time & then Insert into Table.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900