Click here to Skip to main content
12,503,947 members (55,596 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C#
Hi,
Can anyone help me to get the coding for delete button in C# windows form application.When i'm clicking the delete button the entire row should delete.I have created the row in grid view.I have tried the below codes,
private void label5_Click(object sender, EventArgs e)
       {
           DialogResult result = MessageBox.Show("Do You Want Delete selected Row?",
                 "Important",
                 MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question);
 
           if (result == DialogResult.Yes)
           {
               if (dataGridView1.SelectedRows.Count > 0)
               {
                   int selectedIndex = dataGridView1.SelectedRows[0].Index;
                   string D_UserName = dataGridView1[0, selectedIndex].Value.ToString();
                   string nam;
                   DataminingProject.Class1 obj = new DataminingProject.Class1();
                   nam = obj.get();
                   SqlConnection con = new SqlConnection(nam);
                   string sql = "DELETE FROM Usercreations WHERE D_UserName = @ID";
                   SqlCommand deleteRecord = new SqlCommand();
                   deleteRecord.Connection = con;
                   deleteRecord.CommandType = CommandType.Text;
                   deleteRecord.CommandText = sql;
                   SqlParameter RowParameter = new SqlParameter();
                   RowParameter.ParameterName = "@ID";
                   RowParameter.SqlDbType =SqlDbType.VarChar;
                   RowParameter.IsNullable = false;
                   RowParameter.Value = D_UserName;
                   deleteRecord.Parameters.Add(RowParameter);
                   deleteRecord.Connection.Open();
                   deleteRecord.ExecuteNonQuery();
                   deleteRecord.Connection.Close();
                   //add();

               }
 

           }
 

           }


[edit]Code block added - OriginalGriff[/edit]
Posted 5-Oct-12 0:31am
Updated 5-Oct-12 0:34am
v2

1 solution

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

Solution 1

Code as per requierment at click event of datagridview
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int k; // For getting no of rows
            k = dataGridView2.SelectedCells[0].RowIndex;
            D_UserName=dataGridView2.Rows[k].Cells[0].Value.ToString();
            
        }


Code at click event of delete button
private void button11_Click(object sender, EventArgs e)
        {
 
            try
            {
 
                DialogResult dr = MessageBox.Show("Are You Really want to delete dependents Record..", "Dependent Removal Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.No)
                {
                    return;
                }
                else
                {
                    con = new MySqlConnection();
                    con.ConnectionString =ConfigurationSettings.AppSettings["constr"];
                    con.Open();
                    da.DeleteCommand = new MySqlCommand();
                    da.DeleteCommand.CommandText = "DELETE FROM Usercreations WHERE D_UserName = '"+D_UserName+"'";
                    da.DeleteCommand.Connection = con;
                    da.DeleteCommand.ExecuteNonQuery();
                    MessageBox.Show("Record Deleted Succefully");
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error on Deleting " + ex.Message);
            }
        }
  Permalink  
v4
Comments
onurag19 5-Oct-12 8:10am
   
what error you are getting ?
onurag19 6-Oct-12 7:07am
   
Hey you will have to remove getgeridview() Function. That will be not the part of your program.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2016
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