[英]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();
}
显示 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.