Delete data using 2 DialogForm

2019-01-29 09:38发布

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);

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);

and close detaildataform (dialogform2) by

this.Close();

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


Update Progress

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) {

 }

if i want to delete it from dialogform1, i can use

Global.quizOccurrences.removeAT(listBoxQuizzes.SelectedIndex);

but if i want to delete it from dialogform2

Global.quizOccurrences.removeAT(.........); //still not have idea how can i reference index

Update solution from @nitin

so first i write in formdialog2

public Frmdialog1 frm_dialog { get; set; }

then i write this in formdialog1

frmdialog2.frm_dialog=this;

then back again to formdialog1 to write

frm_dialog.quizzes.RemoveAt(frm_dialog.listBoxQuizzes.SelectedIndex);

is that right because i get many error

标签: c# dialog
3条回答
男人必须洒脱
2楼-- · 2019-01-29 10:16

form1 could subscibe to the form2's close event.

inside form1

form2 f2dialog = new form2(/*I guess you are passing data here*/);
f2.dialog.Closing += eventhandler;

somewhere else

void eventhandler(object sender, eventargs e)
{
 //refresh globaldata since by now you have ran delete query
 //rebind or call listbox.items.refresh() or both  <-------------this how do you get data from rver? the server is updated but does global know that?


}

then you need to call code to get data from database again. and rebind to data

listbox.datacontext = Global.GetData();//or however this is done

you have to manually reset this eveerytime you change your database

databinding is not as smart as you think it is.

查看更多
成全新的幸福
3楼-- · 2019-01-29 10:25

If you are opening second dialog from first one u can Have property in Frmdialog2 like

 public Frmdialog1 frm_dialog { get; set; }

After creating object of Frmdialog2 in Frmdialog1 you can set this property as

frmdialog2.frm_dialog=this;

Now u can remove item from this listbox in Frmdialog2 iteself after deleting record from database as

  frm_dialog.quizzes.RemoveAt(frm_dialog.listBoxQuizzes.SelectedIndex);

NOTE: Modifier for your listbox should be public

查看更多
贼婆χ
4楼-- · 2019-01-29 10:28

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

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
查看更多
登录 后发表回答