c# - How to delete the data for a student in the array and list box, and keep the array and listbox synchronized -


i have 2 dimensional array, , add button add values text box list box , have delete button delete selected value list box , array.

this line of code won't work because 2 dimensional array.

names[lstindex] = null;

    string[,] names = new string[10,3];      const int student_name = 0;     const int student_id = 1;     const int major = 2;     private void btnexit_click(object sender, eventargs e)     {          close();     }     private void  assignarray(int row)     {         names[row,student_name] = txtstudentname.text;         names[row, student_id] = txtstdbox.text;         names[row, major] = txtmjbox.text;     }     private string buildstudent(int row)     {         string answser = "";         int upperlimit = names.getupperbound(1);         (int column = 0; column <= upperlimit; column++)         {             answser += names[row, column] + "";         }         return answser;      }       private void btnadd_click(object sender, eventargs e)     {         //find location use array         int position = lststudents.items.count;         assignarray(position);          lststudents.items.add(buildstudent(position));         names[position,major] = txtmjbox.text;         //find last valid subscript              int maxindex = names.getupperbound(0);         //if using last valid  subscript disable add button             if (position == maxindex)             {                 btnadd.enabled = false;             }     }      private void btnupdate_click(object sender, eventargs e)     {         int index = lststudents.selectedindex;         if (index != -1)         {               //update array             assignarray(index);             //names[index, student_name] = txtstudentname.text;             //remove old entry             lststudents.items.removeat(index);             //put new entry in spot                 lststudents.items.insert(index,names[index, student_name]);             //highlight entry user             lststudents.selectedindex = index;         }         else         {         messagebox.show("seleect student, click update","error");         }     }     private void btndelete_click(object sender, eventargs e)     {           int lstindex = lststudents.selectedindex;         //delete data student in array         //and listbox, , keep array , listbox synchronized.        names[lstindex] = null;          lststudents.items.removeat(lstindex);      } 

to "delete" item array you'd have shift indices above index down 1 , shrink array last slot gets discarded. *you use lazy deletion "mark" index deleted know it's safe use (but makes code little more complicated).

are required use multi-dimensional array? switching class represent each student make life easier!...

edit:

    private void btndelete_click(object sender, eventargs e)     {         int lstindex = lststudents.selectedindex;         if (lstindex != -1)         {             //delete data student in array             //and listbox, , keep array , listbox synchronized.             //names[lstindex] = null;              lststudents.items.removeat(lstindex);              // starting @ index remove, copy value next index up, iterate             // shift down replace item being deleted             (int = lstindex; < names.getupperbound(0) - 1; i++)             {                 names[i, student_name] = names[i + 1, student_name];                 names[i, student_id] = names[i + 1, student_id];                 names[i, major] = names[i + 1, major];             }             //clear out last entry:             names[names.getupperbound(0), student_name] = "";             names[names.getupperbound(0), student_id] = "";             names[names.getupperbound(0), major] = "";         }     } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -