如何在textbox和datagridview中检索值

时间:2016-01-17 01:22:24

标签: sql-server vb.net winforms datagridview

我想在textbox中显示记录,只在datagridview中添加一些记录。

存储过程代码

SELECT sh.SchoolYear,
        sh.Levels,sh.Section, sh.DateEnrolled as LatestDate ,
        SI.StudentID,SI.Surname,SI.FirstName,SI.MiddleName, SI.StudAddress , 
        SI.BirthDay,SI.Gender, SI.Nationality, SI.BirthPlace,
        SI.TelNum,SI.SchoolWhereGraduated , 
        SI.DatesWhenGraduated, SI.SchoolLastAttended,
        SI.SchoolAddress, SI.Note,SI.StudImage,
        PI.Father_FirstName,PI.Father_LastName,
        PI.Father_MI,PI.Father_Occupation, 
        PI.Father_TelNUm, PI.Mother_FirstName, PI.Mother_LastName,
        PI.Mother_MI,PI.Mother_Occupation,PI.Mother_TelNum,
        PI.Contact_FirstName,PI.Contact_LastName,PI.Contact_MI,
        PI.Contact_Mobile,PI.Contact_TelNum,PI.Contact_Address  
        FROM StudentInformation SI 
        JOIN StudentHistory SH  
            ON SI.StudentID = SH.StudentID
        JOIN ParentInformation PI
        ON PI.ParentID = SI.ParentID
        WHERE si.StudentID = @studID
        ORDER BY DateEnrolled DESC

vb.net代码

cmd = New SqlCommand("uspstudents", cn)
            cmd.Parameters.AddWithValue("@studID", frmView.dgv1.SelectedCells(0).Value)
            cmd.CommandType = CommandType.StoredProcedure
            da.SelectCommand = cmd
            da.Fill(dt)
            cboSchoolYear.Text = dt.Rows(0)("SchoolYear").ToString
            cboSection.Text = dt.Rows(0).Item("Section").ToString
            cboGradeLevel.Text = dt.Rows(0).Item("levels").ToString
            dtpEnrollment.Text = dt.Rows(0).Item("LatestDate").ToString
            txtStudLN.Text = dt.Rows(0)("SurName").ToString
            txtStudFN.Text = dt.Rows(0)("FirstName").ToString
            txtStudMN.Text = dt.Rows(0)("MiddleName").ToString
            txtAddress.Text = dt.Rows(0)("StudAddress").ToString
            dtpBirthday.Text = dt.Rows(0)("birthday").ToString
            cboNationality.Text = dt.Rows(0)("Nationality").ToString
            txtPlaceOfBirth.Text = dt.Rows(0)("BirthPlace").ToString
            txtStudentCP.Text = dt.Rows(0)("telnum").ToString
            txtSWG.Text = dt.Rows(0)("schoolWhereGraduated").ToString
            dtpDWG.Text = dt.Rows(0)("datesWhenGraduated").ToString
            txtSLA.Text = dt.Rows(0)("schoolLastAttended").ToString
            txtSchoolAddress.Text = dt.Rows(0)("SchoolAddress").ToString
            txtNote.Text = dt.Rows(0)("Note").ToString
            txtFatherGN.Text = dt.Rows(0)("Father_FirstName").ToString
            txtFatherMI.Text = dt.Rows(0)("Father_MI").ToString
            txtFatherLN.Text = dt.Rows(0)("Father_Lastname").ToString
            txtFatherOccupation.Text = dt.Rows(0)("Father_Occupation").ToString
            txtFatherCP.Text = dt.Rows(0)("Father_telnum").ToString
            txtMotherGN.Text = dt.Rows(0)("Mother_FirstName").ToString
            txtMotherLN.Text = dt.Rows(0)("Mother_LastName").ToString
            txtMotherMI.Text = dt.Rows(0)("Mother_MI").ToString
            txtMotherOccupation.Text = dt.Rows(0)("Mother_Occupation").ToString
            txtMotherCP.Text = dt.Rows(0)("Mother_telNum").ToString
            txtContactGN.Text = dt.Rows(0)("Contact_FirstName").ToString
            txtContactLN.Text = dt.Rows(0)("Contact_LastName").ToString
            txtContactMI.Text = dt.Rows(0)("Contact_MI").ToString
            txtContactAddress.Text = dt.Rows(0)("Contact_Address").ToString
            txtContactCP.Text = dt.Rows(0)("Contact_Mobile").ToString
            txtContactTelNum.Text = dt.Rows(0)("Contact_telnum").ToString

显示textbox中的数据效果很好,但问题出在datagridview。我试过的是把这个代码放在我的vb.net代码

dgvHistory.DataSource = dt

但是它会显示我记录中的所有数据,我想要的是只显示schoolyear中的sectionlevelsdatagridview。有人可以帮我解决这个问题。感谢

1 个答案:

答案 0 :(得分:1)

您需要手动为DataGridView创建列。像这样的东西 -

dgvHistory.ColumnCount = 3
dgvHistory.AutoGenerateColumns = False

dgvHistory.Columns(0).Name = "SchoolYear"
dgvHistory.Columns(0).DataPropertyName = "schoolyear"

dgvHistory.Columns(1).Name = "Section"
dgvHistory.Columns(1).DataPropertyName = "Section"

dgvHistory.Columns(2).Name = "Levels"
dgvHistory.Columns(2).DataPropertyName = "levels"

dgvHistory.DataSource = dt