Joe Alegre

Joe Alegre

  • NA
  • 6
  • 0

Delete selected rows on datagrid (WINFORMS)

Nov 24 2008 6:52 PM
Hi everyone,

I am new to C# and I am creating my first database winform application. I have a form with a datagrid and a check box column. What I need to do is be able to delete the selected rows from the database and then refresh the screen if the check box is checked. Below is my code to populate my datagrid

  string conString = "Dsn=MYDSN;Driver={Postgresql};uid=guest;pwd=guest";
            string SQL =
                "select dv_order.id,bp1.description as Vendor,doc_number as OrderNumber, po_number, expected_trans_ts as ShipDate, bp2.description as Shipper, " +
                "CASE WHEN status =1 THEN 'OPEN PENDING' WHEN Status =2 THEN 'OPEN'WHEN Status =3 THEN 'IN PROCESS'WHEN Status =4 THEN 'HOLD' " +
                "WHEN Status =5 THEN 'SHIPPED' WHEN Status =6 THEN 'COMPLETED' ELSE 'OTHER' END " +
                "from dv_order inner join dv_business_partner bp1 on dv_order.vendor_id = bp1.id inner join dv_business_partner bp2 on dv_order.shipper_id = bp2.id " +
                "where (po_number like'PROJECT%'or po_number like 'ADJ%') and status in (1,2,3,4,5,6) and dv_order.type =2 and shipper_id =169294 " +
                "group by dv_order.id,bp1.description, doc_number,po_number,expected_trans_ts,bp2.description, status order by bp1.description";

            OdbcConnection con = new OdbcConnection(conString);
            OdbcCommand cmd = new OdbcCommand(SQL, con);


            DataSet ds = new DataSet();
            con.Open();
            OdbcDataAdapter dad = new OdbcDataAdapter(cmd);
            dad.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            label3.Text = ds.Tables[0].Rows.Count.ToString();
            con.Close();

            dataGridView1.Columns[0].HeaderText = "SELECTED";
            dataGridView1.Columns[1].HeaderText = "ID";
            dataGridView1.Columns[2].HeaderText = "VENDOR";
            dataGridView1.Columns[3].HeaderText = "ORDER";
            dataGridView1.Columns[4].HeaderText = "PO NUMBER";
            dataGridView1.Columns[5].HeaderText = "SHIP DATE";
            dataGridView1.Columns[6].HeaderText = "SHIPPER";
            dataGridView1.Columns[7].HeaderText = "STATUS";
        }

I have done some research on the delete button that needs to be crated and it does not seem towork for me. So far for my delete button i have

       private void button2_Click(object sender, EventArgs e)
        {
           
            DialogResult result;
            string gvIDs = "";
            bool chkbox;

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
              CheckBox DeleteItem = row.Columns["SELECTED"]);

                    ((CheckBox)row.Columns("SELECTED")).Checked = true;

                {
                    gvIDs += ((Label)row.Columns("ID")).Text.ToString() + ",";
                   
                }
            }


            result = MessageBox.Show("Are you sure you would like to Delete Selected?", "Delete", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
               {
                                try
                {
                    string DelSQL= "delete  from dv_order WHERE id = '" + gvIDs.Substring(0, gvIDs.LastIndexOf(",")) + "'";
                    OdbcCommand DelCMD= new OdbcCommand(DelSQL, con);
                    con.Open();
                    DelCMD.ExecuteNonQuery();
                    dataGridView1.DataBind();
                }

Any help would be much appreciated or some sample code on how to make this work?

Thanks in advance.




Answers (3)