Darren_D19
Darren_D19

Reputation: 121

Deleting data from a list from a second form

I have a form that where I read in a csv file containing student information, the file has two columns StudentNumber and Mark. I want to allow the user to click a button on the first form to then go to another form called deleteRecord On this form the user will type in a StudentNumber and a Mark and the record that corresponds with them two pieces of information will be delete from the list.

Since I am new to C# I am not sure on how to go about this so any help will be appreciated.

My list:

public static List<string> studentInfo = new List<string>();

I store all the data from that list in a listbox called lstMarks

I want to also confirm to the user that the record was deleted successfully.

Upvotes: 2

Views: 147

Answers (1)

Voxaim
Voxaim

Reputation: 45

If all the data is stored in the list, simply use LINQ and add a number for each student in the list for the index.

First you'll need to create a class and (I recommend it) put it in a folder. How it looks.

Then you'll have to put the propreties in the class:

public class Student
{
    public int StudentNumber {get; set;}
    public int Mark {get; set;}
    public int Index {get; set;}
}

Now add another class with the list:

partial class MainWindow : Window
{
    private List<Student> _studentInfo = new List<Student>()
    {
        new Student() {Index = 0, StudentNumber = 0, Mark = 0}
        // ...
    }

And then add using at the top of your code of deleteRecord and the name of the folder and the two classes:

using ExampleFolder.Class;

You'll need to call your Student class to be able to modify the StudentNumber and Mark and Index.

Student studentInfo = new Student();
    int iIndex = 0;

    var req = from info in studentInfo
              where info.StudentNumber == txtStudentNumber && info.Mark == txtMarks
              select info.Index; // Starts with 0 for the first student in the list
    foreach(var num in req)
    {
        iIndex = num;
    }
    studentInfo.Remove(studentInfo[iIndex]);

    MessageBox.Show("Deleted!");

Upvotes: 1

Related Questions