簡體   English   中英

如何從UserControl獲取數據到Form? C# WinForms 和 SQLite

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

我知道有人問過這個問題,但不幸的是大多數答案都沒有解決問題。 所以希望有人可以幫助我:)

所以這是我的問題。

我想從StudentLedgerControl獲取這些數據(我用紅色圈起來了)。 然后將此數據傳輸到名為StudentLedgerWindow的表單。

雖然在所有這些之前,必須單擊一個按鈕來顯示StudentLedgerWindow ,一旦顯示,傳輸的數據就會出現。

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(預期結果) 圖片2

顯示 Window 按鈕事件和用戶界面

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

在此處輸入圖像描述

提前謝謝你:) PS我是新來的,所以如果我的帖子有任何問題,請告訴我,以便我可以更改它。

我想我解決了它......我可能......

我所做的只是創建 getter 和 setter( kek,我應該先嘗試一下

然后調用 getter 和 setter。 如果這是有道理的,希望它確實如此

舊代碼

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

新代碼

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

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

謝謝你:)

如果有任何我可以做的代碼改進,請告訴我:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM