简体   繁体   English

如何从UserControl获取数据到Form? C# WinForms 和 SQLite

[英]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).我想从StudentLedgerControl获取这些数据(我用红色圈起来了)。 Then transfer this data to a form called StudentLedgerWindow .然后将此数据传输到名为StudentLedgerWindow的表单。

Although before all of this, a button must be clicked to show the StudentLedgerWindow , which once showed, the transferred data will appear.虽然在所有这些之前,必须单击一个按钮来显示StudentLedgerWindow ,一旦显示,传输的数据就会出现。

StudentLedgerControl.cs 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) StudentLedgerWindow(预期结果) 图片2

Show Window Button Event and User Interface显示 Window 按钮事件和用户界面

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.提前谢谢你:) PS我是新来的,所以如果我的帖子有任何问题,请告诉我,以便我可以更改它。

I think I solved it... idk probably...我想我解决了它......我可能......

All I did is create getters and setters ( kek, I should have tried this first )我所做的只是创建 getter 和 setter( kek,我应该先尝试一下

Then calling the getters and setters.然后调用 getter 和 setter。 ( 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 Getter 和 Setter

    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:)如果有任何我可以做的代码改进,请告诉我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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