現在我從 sql 資料庫中得到了這個:
CourseId: CousreCode
1 0507
4 0508
5 0509
6 0511
7 0512
8 0510
9 0515
因此,在我單擊資料單元格后,它將在文本框中顯示 CourseId 但現在我想獲取與 CourseId 配對的課程代碼值
這是我想要的:

因此,在單擊資料單元格后,它將在文本框中顯示 CourseId 但由于 CourseCode 在另一個表中,我不知道如何將 CourseCode 值顯示到文本框中
這就是我堅持的
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
using (AP2Context context = new AP2Context())
{
tbCourseId.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
tbCourseCode.Text = ...
}
}
uj5u.com熱心網友回復:
使用您的課程 DbSet,您可以撰寫:
tbCourseCode.Text = context.Courses.Find(...).CourseCode;
或(從資料庫重新加載)
tbCourseCode.Text = context.Courses.First(c => c.CourseId == ...).CourseCode;
替換...為具有正確型別的所選課程 CourseId:
- 如果是字串,則重用從為 撰寫的運算式中獲得的值
tbCourseId.Text。 - 如果是 int,例如必要時將其轉換為
intusingint.Parse()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/383404.html
上一篇:如何在Magento2中擴展galley.phtml?
下一篇:Linq如何知道元素的名稱?
