Click here to Skip to main content
11,648,785 members (77,789 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hello,

How to insert,Update and Delete data using datagridview in c# .net windows application?
It should be recede or rearrange ID or Sr. No. after delete any row.

How it can be possible?

Please help me.

Thanks in Advance

Regards
Ankit Agarwal
Software Engineer
Posted 14-Aug-13 0:02am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

how-to-insert-edit-update-and-delete.html[^]

Check the link..hope it will help..
  Permalink  
Comments
ankit.agg1984 at 14-Aug-13 6:12am
   
How to re arrange ID or Sr No. after deleted any rows?
Code-Hunt at 14-Aug-13 6:15am
   
http://www.codeproject.com/Questions/461231/DataGridview-Remove-Row-and-to-Maintain-the-Serial
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

How to Insert, Edit, Update and Delete Data with DataGridView in Windows Form C#.net ||Inserting , Updating and Deleting with DataGridView in Windows forms C#.net Labels: C#.net, GridView, Windows Applications

In this article I am showing to Inserting , Editing , Updating and Deleting options with DataGridview. For that I am Designing form with two textboxes with Name and Location ,DataGridview to display data and four buttons to Save , Edit , Update and Delete. To do this just follow below steps:

  •     In form load I am binding the data from database.
    
  •     In save button click event saving data to database which are inserted into the name and location textboxes.
    
  •     In Delete button click event  Deleting the selected row data in DataGridview from database.
    
  •     In Edit button Click event filling the selected data from Gridview into Name and location textboxes.
    
  •     In Update Button click event updating data which are edited the name and location textboxes.
    

Write the following code in Form.cs : Form.cs 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.Configuration; using System.Data.SqlClient;

namespace savedata { public partial class Form1 : Form {

    SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["Sqlcon"].ConnectionString);
    public Form1()
    {
        InitializeComponent();
        Bind();

    }

    private void Clear()
    {
        txtName.Text = string.Empty;
        txtLocation.Text = string.Empty;
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert Into Test_Data(Name,Location) Values (@Name,@Location)", con);
        cmd.Parameters.AddWithValue("Name", txtName.Text);
        cmd.Parameters.AddWithValue("Location", txtLocation.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("Inserted sucessfully");
        Bind();
        Clear();
    }

    private void Bind()
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from Test_Data", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        con.Close();
    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        SqlCommand delcmd = new SqlCommand();
        if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
        {
            delcmd.CommandText = "DELETE FROM Test_Data WHERE ID=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
            con.Open();
            delcmd.Connection = con;
            delcmd.ExecuteNonQuery();
            con.Close();
            dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
            MessageBox.Show("Row Deleted");
        }
        Bind();
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Update Test_Data set Name=@Name,Location=@Location Where(Name=@Name)", con);
        cmd.Parameters.AddWithValue("@Name", txtName.Text);
        cmd.Parameters.AddWithValue("@Location", txtLocation.Text);
        cmd.ExecuteNonQuery();
        MessageBox.Show("updated......");
        con.Close();
        Bind();
        Clear();
    }

    private void btnEdit_Click_1(object sender, EventArgs e)
    {
        int i;
        i = dataGridView1.SelectedCells[0].RowIndex;
        txtName.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
        txtLocation.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
    }
}

}

Then run the application you will get output like below:

  Permalink  

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

  Print Answers RSS
0 CPallini 370
1 F-ES Sitecore 364
2 OriginalGriff 324
3 Kornfeld Eliyahu Peter 324
4 Sergey Alexandrovich Kryukov 254
0 OriginalGriff 1,512
1 Sergey Alexandrovich Kryukov 1,022
2 jyo.net 994
3 DamithSL 986
4 CPallini 945


Advertise | Privacy | Mobile
Web02 | 2.8.150804.4 | Last Updated 6 May 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100