Home > front end >  linq get value from Id
linq get value from Id

Time:12-17

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:

enter image description here

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 = ...
            }
        }

CodePudding user response:

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 int using int.Parse() if necessary for example.
  • Related