0

i want to delete the row of data from the database, currently i can delete the row of data by clicking on the delete button, but the database Table is not updated, how do i do so?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace project
{
    public partial class frmTestPrint : Form
    {
        public frmTestPrint()
        {
            InitializeComponent();
        }

        private void frmTestPrint_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed.
            this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);

        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
        }
    }
}
bandaa
  • 159
  • 3
  • 5
  • 11
  • If you are trying to delete the row from the database as well, don't you think it would be a good idea to post your SQL Command code as well? As it is, you have only posted the code that affects the `datagridview`. – Brian Apr 08 '13 at 17:23
  • i have no SQL command... i used the datagridview and chose the datasource through the properties... – bandaa Apr 08 '13 at 17:27
  • is that the problem? is that why i cant delete and update the SQL database table? – bandaa Apr 08 '13 at 17:28
  • No necessarily, no. It was just a suggested starting point. What you need to do is not just update your `datagridview` but the database as well. See the answer below. – Brian Apr 08 '13 at 17:29
  • thanks. and the answer below is some what confussing there are many answers in the question... which one is the best to guide me? – bandaa Apr 08 '13 at 17:32

4 Answers4

1
private void Delete()
    {

        int i = 0;
        con1 = getConnection();
        con1.Open();
        SqlCommand storedProcedureCommand = con1.CreateCommand();
        storedProcedureCommand.CommandType = CommandType.Text;
        storedProcedureCommand.CommandText = "DELETE FROM name of your table WHERE pkeyID = @pkeyID";

        SqlParameter inparam2 = new SqlParameter("@pkeyID", SqlDbType.Int);
        inparam2.Direction = ParameterDirection.Input;
        inparam2.Value = Convert.ToInt32(dataGridView2.Rows[i].Cells[0].Value);
        storedProcedureCommand.Parameters.Add(inparam2);
        storedProcedureCommand.ExecuteNonQuery();

    }    
Bane
  • 25
  • 8
  • of course change the name of your datagrid,plus find in witch cell is your pkeyID,then call loaddata() method after that and thats it! – Bane Apr 19 '13 at 07:42
  • looks good, im currently working on other things, but i will have a look at this again and if it works will mark your reply as correct answer :) cheers – bandaa Apr 29 '13 at 22:09
1

This is only to delete one at a time. I am still working on multiple deletes.

        if (MessageBox.Show("Sure you wanna delete?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
        {
            //get the index from the dataGridView
            int rowIndex = table1DataGridView.CurrentCell.RowIndex;
            //Remove from both the actual database & datagridview
            table1BindingSource.RemoveAt(rowIndex);
            //update table 1
            this.table1TableAdapter.Update(this.maquinasDataSet.Table1);
            //load table 1
            this.table1TableAdapter.Fill(this.maquinasDataSet.Table1);
        }
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
0

You're not actually impacting the database at all. Perhaps this will help:

How to delete a selected DataGridViewRow and update a connected database table?

Have you explored the .Update method you get with the TableAdapter? I think that should cover what you're after. http://msdn.microsoft.com/en-us/library/bz9tthwx(v=vs.100).aspx

Does this work:

private void btnDelete_Click(object sender, EventArgs e)
    {
        dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
        this.bookingTableAdapter.Update(this.usersDataSet1.Booking);
    }

if that doesn't work, you're going to have to come back w/ an error message or hopefully someone who knows what they're doing can come bail me out...

Community
  • 1
  • 1
sous2817
  • 3,915
  • 2
  • 33
  • 34
  • i have looked at the link, cant seem to find a real solution that helps? non of the code deletes my data from the DB – bandaa Apr 08 '13 at 17:40
  • I think the last one should get you close...or maybe have a look here: http://msdn.microsoft.com/en-us/library/xzb1zw3x.aspx – sous2817 Apr 08 '13 at 17:45
  • its not working, or not enough guideance shall i say, do you know if you can explain the steps better? thanks – bandaa Apr 08 '13 at 21:37
  • @bandaa added a bit more to my answer. Hopefully that will get you there (if it's indeed the correct answer). – sous2817 Apr 08 '13 at 23:50
  • i had tried something similar to that... but i have tried your answer, the problem is, it does not remove the data from the table, it still just deletes the data from the datagridview UI but not from the database. – bandaa Apr 09 '13 at 16:35
  • @bandaa Sorry, I'm fresh out of ideas. What I posted above works on my test project. The only thing I can think of is that what you have bound to dataGridView1 is something other than what bookingTableAdapter is filling. Are you getting any error message at all? Perhaps you're violating some key constraint or something like that... – sous2817 Apr 09 '13 at 16:59
  • no i have no error messages, i know you are trying your best, i appreciate that. i just dont seem to know what to do now. – bandaa Apr 09 '13 at 17:23
  • ok...last stab. Are you sure the code is actually being executed? can you put a break point or something @ the .Update line to make sure it's actually being hit? – sous2817 Apr 09 '13 at 17:33
  • what isit i should be loking for when i put the break point in? i have executed it witht he break point at .Update line and it seems to be doing that step.... but for some reason the database is not updating. iv even deleted the previous datagridvie and started over fresh :\ – bandaa Apr 09 '13 at 17:44
0

Delete a value from the data grid using DataGridviewBUttonColumn

private void delete_record()
    {

         if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
        {
            try
            {
                //am taking connection string using a class called "sqlconnection_imventory()"
                SqlConnection conn = Connection.sqlconnection_imventory();
                //Cell[0] is my button column & Cell[1] is SID coumn
                string a = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                string sql = "DELETE FROM grades WHERE SID='" + a + "'";
                conn.Open();
                SqlCommand delcmd = new SqlCommand(sql, conn);
                delcmd.Connection = conn;
                delcmd.ExecuteNonQuery();
                conn.Close();
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                MessageBox.Show("deleted");
            }
            catch (Exception)
            {

                throw;
            } 
        }
    }