Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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
Edited 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 at 5-Oct-12 8:10am
   
what error you are getting ?
onurag19 at 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
0 Sergey Alexandrovich Kryukov 553
1 George Jonsson 258
2 OriginalGriff 235
3 CPallini 210
4 PIEBALDconsult 155
0 OriginalGriff 5,865
1 Sergey Alexandrovich Kryukov 5,316
2 CPallini 4,760
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


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