简体   繁体   中英

How to obtain data from UserControl to Form? C# WinForms and SQLite

I know that this question has been asked, but unfortunately most of the answers did not solve the problem. So hopefully someone can help me:)

So here is my problem.

I want to get this data from StudentLedgerControl (I encircled it with red). Then transfer this data to a form called StudentLedgerWindow .

Although before all of this, a button must be clicked to show the StudentLedgerWindow , which once showed, the transferred data will appear.

StudentLedgerControl.cs

public void LoadStudentLedger(SQLiteConnection conn)
{
        SQLiteCommand sqlite_cmd;

        sqlite_cmd = new SQLiteCommand("SELECT * FROM Student", conn);
        SQLiteDataReader read = sqlite_cmd.ExecuteReader();

        StudentFlowPanel.SuspendLayout();
        StudentFlowPanel.Controls.Clear();

        while (read.Read())
        {
            sc = new StudentControl();
            sc.StudentIDLabel.Text = "Student ID: " + read.GetInt32(0).ToString(); // id
            sc.StudentNameLabel.Text = read.GetString(1) + " " + read.GetString(2) + " " + read.GetString(3); // fullname
            sc.StudentSectionLabel.Text = "Section: " + read.GetString(4); // section  
            sc.StudentLevelLabel.Text = "Level: " + read.GetInt32(5).ToString(); // level
            StudentFlowPanel.Controls.Add(sc);
        }
        
        
        StudentFlowPanel.ResumeLayout();  
}

StudentLedgerWindow (expected outcome) 图片2

Show Window Button Event and User Interface

private void ViewLedgerButton_Click(object sender, EventArgs e)
{
    // Once clicked, the data should show on StudentLedgerWindow
}

在此处输入图像描述

Thank You in Advance:) PS I'm new here, so if there are any problems with my post pls tell me so that I can change it.

I think I solved it... idk probably...

All I did is create getters and setters ( kek, I should have tried this first )

Then calling the getters and setters. ( if that makes sense, hopefully it does )

Old Code

public void LoadStudentLedger(SQLiteConnection conn)
    {
        SQLiteCommand sqlite_cmd;

        sqlite_cmd = new SQLiteCommand("SELECT * FROM Student", conn);
        SQLiteDataReader read = sqlite_cmd.ExecuteReader();

        StudentFlowPanel.SuspendLayout();
        StudentFlowPanel.Controls.Clear();

        while (read.Read())
        {
            sc = new StudentControl();
            sc.StudentIDLabel.Text = "Student ID: " + read.GetInt32(0).ToString(); // id
            sc.StudentNameLabel.Text = read.GetString(1) + " " + read.GetString(2) + " " + read.GetString(3); // fullname
            sc.StudentSectionLabel.Text = "Section: " + read.GetString(4); // section  
            sc.StudentLevelLabel.Text = "Level: " + read.GetInt32(5).ToString(); // level

            StudentFlowPanel.Controls.Add(sc);
        }

        StudentFlowPanel.ResumeLayout();
    }

New Code

public void LoadStudentLedger(SQLiteConnection conn)
    {
        SQLiteCommand sqlite_cmd;

        sqlite_cmd = new SQLiteCommand("SELECT * FROM Student", conn);
        SQLiteDataReader read = sqlite_cmd.ExecuteReader();

        StudentFlowPanel.SuspendLayout();
        StudentFlowPanel.Controls.Clear();

        while (read.Read())
        {
            sc = new StudentControl();
            sc.StudentId = "Student ID: " + read.GetInt32(0).ToString(); // id
            sc.StudentName = read.GetString(1) + " " + read.GetString(2) + " " + read.GetString(3); // fullname
            sc.StudentSection = "Section: " + read.GetString(4); // section  
            sc.StudentLevel = "Level: " + read.GetInt32(5).ToString(); // level

            sc.StudentIDLabel.Text = "Student ID: " + read.GetInt32(0).ToString(); // id
            sc.StudentNameLabel.Text = read.GetString(1) + " " + read.GetString(2) + " " + read.GetString(3); // fullname
            sc.StudentSectionLabel.Text = "Section: " + read.GetString(4); // section  
            sc.StudentLevelLabel.Text = "Level: " + read.GetInt32(5).ToString(); // level

            StudentFlowPanel.Controls.Add(sc);
        }

        StudentFlowPanel.ResumeLayout();
    }

Getters and Setters

    public string _StudentName;
    public string _StudentSection;
    public string _StudentLevel;
    public string _StudentId;

    public string StudentName
    {
        get { return _StudentName; }
        set { _StudentName = value; }
    }
    public string StudentSection
    {
        get { return _StudentSection; }
        set { _StudentSection = value; }
    }
    public string StudentLevel
    {
        get { return _StudentLevel; }
        set { _StudentLevel = value; }
    }
    public string StudentId
    {
        get { return _StudentId; }
        set { _StudentId = value; }
    }

Thank You:)

And also if there are any code improvements I can do please let me know:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM