Thursday 26 June 2014

insert update delete in DataGridView in window application with C#





Description:

            In this example we explain that how to create Insert,Update,Delete in DataGrid View in C# window application. We already explain many example like insert update delete with paging and modal popup in web application. But in this example we explain that in how to perform CRUD operation in datagrid view in window application with C#.

To do this first you have to create one table in which you perform the operation.

And second main thing is that to perform Edit and Delete operation in datagrid view in window application is different from web Gridview. Here you have to create the dataGridView2_CellContentClick event like below

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)




ln which event you have to define the logic of the Edit and Delete operation in Gridview.in this application we also provide the paging facility in datagrid view in window application with c#.

to show code for paging in Datagridview in window form application Paging in Datagrid view in C#

Render Partial view with webgrid in modal popup in MVC Render partial view in modal popup

Here is code that demonstrate CRUD operation in datagrid view in window application

//Form Load Event

private void cardetail_Load(object sender, EventArgs e)
        {

            dataGridView2.DataSource = binddata();
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.AllowUserToAddRows = false;


           

            DataGridViewLinkColumn Editlink = new DataGridViewLinkColumn();
            Editlink.UseColumnTextForLinkValue = true;
            Editlink.HeaderText = "Edit";
            Editlink.DataPropertyName = "lnkColumn";
            Editlink.LinkBehavior = LinkBehavior.SystemDefault;
            Editlink.Text = "Edit";

            dataGridView2.Columns.Add(Editlink);
            DataGridViewLinkColumn Deletelink = new DataGridViewLinkColumn();
            Deletelink.UseColumnTextForLinkValue = true; Deletelink.HeaderText = "delete";
            Deletelink.DataPropertyName = "lnkColumn";
            Deletelink.LinkBehavior = LinkBehavior.SystemDefault;
            Deletelink.Text = "Delete";
            dataGridView2.Columns.Add(Deletelink);

            DataGridViewLinkColumn Printlink = new DataGridViewLinkColumn();
            Printlink.UseColumnTextForLinkValue = true; Deletelink.HeaderText = "print";
            Printlink.DataPropertyName = "lnkColumn";
            Printlink.LinkBehavior = LinkBehavior.SystemDefault;
            Printlink.Text = "Print";
            dataGridView2.Columns.Add(Printlink);

        }

//Binding Gridview

public void binddata()
        {
            DataTable dt = new DataTable();
            string query = "select * from cardetail";

            SqlCommand cmd = new SqlCommand(query, cn);
            SqlDataAdapter sa = new SqlDataAdapter();
            sa.SelectCommand = cmd;

            cn.Open();
            cmd.ExecuteNonQuery();
            sa.Fill(dt);
            dataGridView2.DataSource = dt;
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.AllowUserToAddRows = false;

            cn.Close();




        }

//Edit Delete Event

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            SqlCommand cmd;
            SqlCommand Command;
            SqlDataReader Reader;
            try
            {
                empid = Convert.ToInt32(dataGridView2.Rows[e.RowIndex].Cells["id"].Value);
                switch (e.ColumnIndex)
                {
                    case 9:
                        cn.Open();
                        Command = new SqlCommand("Select * from cardetail where id='" + empid + "'", cn);
                        Reader = Command.ExecuteReader();
                        if (Reader.HasRows)
                        {
                            if (Reader.Read())
                            {
                                textBox8.Text = Reader.GetValue(1).ToString();

                                textBox9.Text = Reader.GetValue(2).ToString();
                                textBox10.Text = Reader.GetValue(3).ToString();
                                textBox11.Text = Reader.GetValue(4).ToString();
                                textBox12.Text = Reader.GetValue(5).ToString();
                                textBox13.Text = Reader.GetValue(6).ToString();
                                textBox14.Text = Reader.GetValue(7).ToString();
                                button4.Visible = true;
                                button3.Visible = false;
                            }

                        }
                        cn.Close();
                        break;

                    case 10:
                        cn.Open();
                        cmd = new SqlCommand("delete from cardetail where id = '" + empid + "'", cn);
                        cmd.ExecuteNonQuery();
                        cn.Close();
                        MessageBox.Show("Deleted Successfully.....");
                         binddata();
                      
                        break;
                    case 11:
                        cn.Open();

                        Command = new SqlCommand("Select * from cardetail where id='" + empid + "'", cn);
                        Reader = Command.ExecuteReader();
                        GenerateBillAndExport(Reader);
                        cn.Close();


                        break;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

//Update Button Click

private void button4_Click(object sender, EventArgs e)
        {

            string a = DateTime.Now.ToShortDateString();
            string query = "update cardetail set carno='" + textBox8.Text + "',carname='" + textBox9.Text + "',address='" + textBox10.Text + "',personname='" + textBox11.Text + "',cellno='" + textBox12.Text + "',detail='" + textBox13.Text + "',total='" + Convert.ToDouble(textBox14.Text) + "' where id='" + empid + "'";

            SqlCommand cmd = new SqlCommand(query, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Record Updates Successfully.....");
            cn.Close();
            clear();
            binddata();
          
 button3.Visible = true;
            button4.Visible = false;

        }

1 comments:

  1. Amazing post keep it up bro..........

    ReplyDelete