示例#1
0
        private void button2_Click(object sender, EventArgs e)
        {
            //new MessageBox()
            List <String> rows = new List <String>();

            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                rows.Add(String.Format("{0}행(id={1})", row.Cells[0].RowIndex + 1, row.Cells[0].Value));
            }
            if (rows.Count == 0)
            {
                MessageBox.Show("어떤행도 제대로 선택되지 않았습니다", "삭제불가");
                return;
            }
            DialogResult result1 = MessageBox.Show(String.Join(" , ", rows) + "을 삭제하시겠습니까?", "의사확인",
                                                   MessageBoxButtons.YesNo);

            if (result1 == DialogResult.No)
            {
                return;
            }
            SQLiteConnection conn = srvmgr.getSingleConnection();
            SQLiteCommand    cmd  = new SQLiteCommand(conn);

            using (var tran = conn.BeginTransaction())
            {
                try
                {
                    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    {
                        //get key
                        int rowId = Convert.ToInt32(row.Cells[0].Value);

                        //avoid updating the last empty row in datagrid
                        if (rowId > 0)
                        {
                            //delete
                            //aController.Delete(rowId);
                            cmd.CommandText = string.Format("DELETE FROM hosts Where id={0}", rowId);
                            cmd.ExecuteNonQuery();
                            //refresh datagrid
                            dataGridView1.Rows.RemoveAt(row.Index);
                        }
                    }
                    tran.Commit();
                }catch (Exception ex)
                {
                    tran.Rollback();
                }
            }

            srvmgr.CloseSingle();
        }