问题描述
您好,我有关于从 2 个表单对话框中删除数据的问题
在第一个对话框中,它包含数据列表,在第二个对话框中,它包含数据的详细信息和删除按钮...我已经成功删除了数据库中的数据,但我很困惑如何从列表中删除数据...
hi i have question for remove data from 2 forms dialog
in first dialogform it's contain list of data, and in second dialogform it's contain detail of data and delete button... i already successfully delete data in database but i confused how to remove data from list...
如果只是选择数据并删除,我知道可以使用此代码完成
if just select data and delete i know it's can be done with this code
quizzes.RemoveAt(listBoxQuizzes.SelectedIndex);
但是dialogform1中的问题在这里没有可用的按钮删除,只查看详细数据.所以如果用户想删除数据,他必须打开 dialogform2(详细数据)我已经使用此代码删除了数据库中的数据
but problem here in dialogform1 not available button delete, just view details data. so if user want to delete data, he must open dialogform2 (detail data) i already done delete data in database with this code
Global.deleteData("DELETE FROM Quiz_Occurrences WHERE ID = " + id);
并通过
this.Close();
并移动到 dialogform1 (listdatabox)这里的问题,我刚刚删除的数据仍然在那里,因为它还没有删除(已经从数据库中删除但没有从列表中删除).需要重启程序才能看到删除数据的效果
and move to dialogform1 (listdatabox) the problem in here, data which just i delete still in there because it's still not remove yet (already delete from database but not remove from list). and need to restart program to see effect of delete data
更新进度
我将数据更改为全局变量,所以从技术上讲,我可以删除 dialogform2 中的数据
这是代码(dialogform1 中的修饰符列表框)
i changed data to global var, so it's technically i can remove data in dialogform2
this is code (modifier listbox in dialogform1)
int no = 1;
foreach (CQuizOccurrence myQuizOccurrence in Global.quizOccurrences) {
}
如果我想从 dialogform1 中删除它,我可以使用
if i want to delete it from dialogform1, i can use
Global.quizOccurrences.removeAT(listBoxQuizzes.SelectedIndex);
但是如果我想从 dialogform2 中删除它
but if i want to delete it from dialogform2
Global.quizOccurrences.removeAT(.........); //still not have idea how can i reference index
<小时>
更新来自@nitin的解决方案
Update solution from @nitin
所以我首先在formdialog2中写
so first i write in formdialog2
public Frmdialog1 frm_dialog { get; set; }
然后我在formdialog1中写这个
then i write this in formdialog1
frmdialog2.frm_dialog=this;
然后再回到formdialog1写
then back again to formdialog1 to write
frm_dialog.quizzes.RemoveAt(frm_dialog.listBoxQuizzes.SelectedIndex);
是这样吗,因为我得到了很多错误
is that right because i get many error
推荐答案
在问了很多关于这个话题的不同问题后,我终于可以做我想做的事了
首先我尝试将 var 更改为 global,这样我就可以从 dialogform2 中删除列表框 dialogform1 中的数据(我认为这是最简单的方法)
i'm finally be able to do as i want after ask many different question about this topic
first i try to change var to global, so i can remove data in listbox dialogform1 from dialogform2 (i think it's the easiest way)
//in dialog form1
foreach (CQuizOccurrence myQuizOccurrence in Global.quizOccurrences) {
//load data from Global.quizOccurences
}
//call function close to close dialogform1
然后在 dialogform2 中,将 Global.quizOccurrences 数据与日期和时间详细数据匹配(使用列表和 foreach)
then in dialogform2, match Global.quizOccurrences data with date&time detail data (using list & foreach)
List<CQuizOccurrence> matchData = new List<CQuizOccurrence>();
foreach (CQuizOccurrence myQuizOccurrence in Global.quizOccurrences)
{
DateTime dtDatabase = (DateTime)myQuizOccurrence.occurred;
string dt = dtDatabase.ToString();
if (dt == dateOccur) {
matchData.Add(myQuizOccurrence);
}
}
foreach (CQuizOccurrence myQuizOccurrence in matchData)
{
Global.quizOccurrences.Remove(myQuizOccurrence);
}
//call function show dialog for formdialog1
这篇关于使用 2 DialogForm 删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!