首先从列表框中删除数据然后从数据库中

时间:2014-02-04 07:23:08

标签: c# listbox

我有测验&问题表格。 一个测验可以容器可以容纳两个或更多个问题, 所以流程将是:

测验表格 - 选择测验 - >问题表格 - CRUID问题 - 确定/取消 - 测验表格

我的问题是,当我删除问题表单中的问题时,我按“取消”。问题仍然从数据库中删除,因为我使用了这段代码:

Global.deleteData("DELETE FROM Questions WHERE content= '" + var + "'");
Global.questions.RemoveAt(listBoxQuestions.SelectedIndex);
updateListBoxQuestions();

我想要的是当用户点击删除按钮时,程序只是从列表中删除数据,因此数据尚未从数据库中删除。

Global.questions.RemoveAt(listBoxQuestions.SelectedIndex);
updateListBoxQuestions();

当用户按下ok按钮时,程序开始使用以下代码删除从数据库中删除的列表数据:

Global.deleteData("DELETE FROM Questions WHERE content= '" + var + "'");

怎么做?


我的按钮
enter image description here

4 个答案:

答案 0 :(得分:1)

Global.questions.RemoveAt(listBoxQuestions.SelectedIndex);
updateListBoxQuestions();
DialogResult dialogResult = MessageBox.Show("are you Sure", "question", MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
    Global.deleteData("DELETE FROM Questions WHERE content= '" + var + "'");

}

答案 1 :(得分:1)

假设Global.questions是字符串列表,用户可以在按 [确定] 之前删除多个项目,您可以尝试定义另一个字符串变量列表以保留已删除的问题(变量范围)取决于你,只要它可以在按钮ok事件处理程序中访问:

Global.deletedQuestions.Add(Global.questions[listBoxQuestions.SelectedIndex]);
Global.questions.RemoveAt(listBoxQuestions.SelectedIndex);
updateListBoxQuestions();

然后按下确定按钮:

var tobeDeleted = "'" + string.Join("','", Global.deletedQuestions) + "'"
Global.deleteData("DELETE FROM Questions WHERE content IN (" + tobeDeleted + ")");
Global.deletedQuestions.Clear();

答案 2 :(得分:1)

将此值放在全局

List<string> lst_tempValue = new List<string>();

在您的函数中

Global.questions.RemoveAt(listBoxQuestions.SelectedIndex) ; // removed from the list box.
lst_tempValue.add(listBoxQuestions.SelectedValue); // add into memory.
updateListBoxQuestions(); // do somethings...

然后当您点击“确定”按钮时,您可以删除临时删除的所有记录。

foreach(string s_value in lst_tempValue) // foreach the temporary list
Global.deleteData("DELETE FROM Questions WHERE content= '" + s_value 

 + "'"); // delete what ever stored in the memory

答案 3 :(得分:0)

您可以在表单上使用对话框而不是“确定”按钮。作为,

if (MessageBox.Show("Are you sure to delete current record", "Delete Record?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
    Global.deleteData("DELETE FROM Questions WHERE content IN (" + tobeDeleted + ")");
}