1

I have a datagridview that adds the table data using code, I want to delete a row of data and have it update in the database table also. How do I do this? Any tips?

The code is presented below:

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;
using System.Data.Sql;
using System.Data.SqlClient;


namespace project
{
    public partial class frmTestPrint : Form
    {
        //with code
        SqlConnection cn = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=F:etc");
        SqlDataAdapter da;
        DataTable dt = new DataTable();


        public frmTestPrint()
        {
            InitializeComponent();
        }

        //with code
        private void BindDataGridView2()
        {
            string command = "select * from booking";
            da = new SqlDataAdapter(command,cn);
            da.Fill(dt);
            dataGridView2.DataSource = dt;


        }

        private void frmTestPrint_Load(object sender, EventArgs e)
        {

            //with code
            BindDataGridView2();
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {


        }
    }
}

I have tried many different ways and am stuck, I really need some assistance to guide me through this so if anyone can assist please do.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bandaa
  • 159
  • 3
  • 5
  • 11
  • http://stackoverflow.com/questions/2084346/delete-selected-row-from-datagridview-and-update-db?rq=1 – PW Kad Apr 09 '13 at 18:59
  • @kadumel i have tried the link, and many other links, it hasn't helped, the data is just not deleting from the database! – bandaa Apr 09 '13 at 19:01
  • I don't want to discourage you, but it appears that you are using direct SQL statements, which is fine if that is what you want to use. If you are going to delete data from the database then you need to run a SQL DELETE statement on that record, if you Google how to do that you will find several examples. Keep in mind that A: You have to be very careful deleting data from a database, especially with a SQL statement (you can screw things up fast) and B: There are a lot of other options for how to handle CRUD functions. I would Google 'Using Entity Framework with WinForms if I were you. – PW Kad Apr 09 '13 at 19:23
  • Deletion of the field in `DataGridView` should NOT trigger a database update. `DataGridView` is a mere view/representation of the data. – Victor Zakharov Apr 09 '13 at 20:05

2 Answers2

1

Upon further review it looks like you are asking a LOT of questions on StackOverflow about basic operations in WinForms. My recommendation for you is to seek out materials to learn how to program in whichever languages and frameworks you choose and to try to understand what the code you are writing is doing.

When you ask for everyone to write the code for you you are just compounding the issue - you don't fully understand what your code is doing and it is difficult to fix the errors. In my past I have done this as well and spent 10x time trying to get other peoples' code to work for me than learning to write it for myself.

I know that your initial thought is that you will figure it out eventually by asking others to write your code but in my experience this is not the case and you aren't learning anything this way.

PW Kad
  • 14,953
  • 7
  • 49
  • 82
  • +1. I generally agree with the point made here. As a side note, if it only takes you 10x the time to get other people's code to work, you are lucky. It sometimes takes me 100x to do that. :) – Victor Zakharov Apr 09 '13 at 20:03
  • i do understand you, but the questions iv been asking have been about the same problem, getting this damn delete button to work! lol. ill take your advice though and start seeking other sources, but i am new and just want to complete this task that i am doing making a program for my friend. – bandaa Apr 09 '13 at 20:50
  • I see other questions such as how to query data from a database, how to load it into the DataGridView, etc... which leads me to believe you aren't using any other resources. I hear you on wanting to get it done but it is very hard to help you at this point. See if this link gives you any direction http://www.codeproject.com/Articles/1155/Simple-ADO-NET-Database-Read-Insert-Update-and-Del – PW Kad Apr 09 '13 at 20:55
0

Try this,

1st, you need to know what row you need to delete(the ID). So try:

string myID = dataGridView2.CurrentRow.Cells["ID"].Value

Next you need to update your Database. Now you can do it has you have using a string command, but i wouldn't do this, i would use a stored procedure. So in your sql:

CREATE PROCEDURE myDeleteQuery
@paramID
AS
DELETE * FROM booking WHERE booking.ID = @param

really simple delete query, not knowing your table structure im just presuming.

last thing you want to do is fix it all together so try this.

using (SqlConnection AutoConn = new SqlConnection(cn))
{
    AutoConn.Open();
    using (SqlCommand InfoCommand = new SqlCommand())
    {
        using (SqlDataAdapter infoAdapter = new SqlDataAdapter(InfoCommand))
        {
            InfoCommand.Connection = AutoConn;
            InfoCommand.CommandType = CommandType.StoredProcedure;
            InfoCommand.CommandText = "myDeleteQuery";
            InfoCommand.Parameters.AddWithValue("@paramID", myID);
            InfoCommand.CommandTimeout = 180;
            InfoCommand.ExecuteScalar()
            AutoConn.Close();
        }
    }
}

This will update your table. Now all you need to do is show the change so just recall your

BindDataGridView2();

And your sorted.

Hope This helps you and I Read your question correctly

lemunk
  • 2,616
  • 12
  • 57
  • 87