private SemesterSubject FillSemesterObject()
        {
            SemesterSubject obj = new SemesterSubject();

            obj.SemesterID   = (int)cmbSemesterSelect.SelectedValue;
            obj.SemesterName = cmbSemesterSelect.Text.ToString();
            obj.SubjectID    = (int)cmbSubjectSelect.SelectedValue;
            obj.SubjectName  = cmbSubjectSelect.Text.ToString();
            return(obj);
        }
        public void FillToGrid()
        {
            List <SemesterSubject> alreadyGridList    = (List <SemesterSubject>)dgvSemesterSubject.DataSource;
            SemesterSubject        objSemesterSubject = FillSemesterObject();

            if (alreadyGridList != null)
            {
                alreadyGridList.Add(objSemesterSubject);
            }
            else
            {
                alreadyGridList = new List <SemesterSubject>();
                alreadyGridList.Add(objSemesterSubject);
            }
            dgvSemesterSubject.DataSource = null;
            dgvSemesterSubject.DataSource = alreadyGridList;

            // an alternative way to get grid values to a list.

            //List<SemesterSubject> lstSemesterSubject = new List<SemesterSubject>();
            //SemesterSubject objSemesterSubject = new SemesterSubject();

            //if(dgvSemesterSubject.DataSource != null)
            //{
            //    lstSemesterSubject = (List<SemesterSubject>)dgvSemesterSubject.DataSource;
            //    objSemesterSubject.SemesterID = Convert.ToInt32(cmbSemesterSelect.SelectedValue);
            //    objSemesterSubject.SemesterName = cmbSemesterSelect.Text;
            //    objSemesterSubject.SubjectID = Convert.ToInt32(cmbSubjectSelect.SelectedValue);
            //    objSemesterSubject.SubjectName = cmbSubjectSelect.Text;
            //    lstSemesterSubject.Add(objSemesterSubject);

            //    dgvSemesterSubject.AutoGenerateColumns = false;
            //    dgvSemesterSubject.DataSource = null;
            //    dgvSemesterSubject.DataSource = lstSemesterSubject;
            //}
            //else
            //{
            //    objSemesterSubject.SemesterID = Convert.ToInt32(cmbSemesterSelect.SelectedValue);
            //    objSemesterSubject.SemesterName = cmbSemesterSelect.Text;
            //    objSemesterSubject.SubjectID = Convert.ToInt32(cmbSubjectSelect.SelectedValue);
            //    objSemesterSubject.SubjectName = cmbSubjectSelect.Text;
            //    lstSemesterSubject.Add(objSemesterSubject);

            //    dgvSemesterSubject.AutoGenerateColumns = false;
            //    dgvSemesterSubject.DataSource = null;
            //    dgvSemesterSubject.DataSource = lstSemesterSubject;
            // }
        }