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();
}
}
}
[edit]Code block added - OriginalGriff[/edit]