now i have this from sql database:
CourseId: CousreCode
1 0507
4 0508
5 0509
6 0511
7 0512
8 0510
9 0515
so after i clicked the data cell it will show the CourseId in the textbox but now i want to get the course code value that it is pair with the CourseId
here’s what i want:

so after i click a data cell it will show the CourseId in the text box
but since CourseCode is in another table i dont know how to show the CourseCode value to the text box
here’s what i’m stuck at
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 = ...
}
}
>Solution :
Using your Courses DbSet, you can maybe write:
tbCourseCode.Text = context.Courses.Find(...).CourseCode;
or (to reload from Database)
tbCourseCode.Text = context.Courses.First(c => c.CourseId == ...).CourseCode;
Replace ... with the selected course CourseId with the right type:
- If string, reuse the value you got from the expression you write for
tbCourseId.Text. - If int, convert it to
intusingint.Parse()if necessary for example.