如何在表中保存Combobox.selectedvalue并在datagridview中显示Display成员 [英] How to Save Combobox.selectedvalue in table and display the Display member in datagridview

查看:74
本文介绍了如何在表中保存Combobox.selectedvalue并在datagridview中显示Display成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子

tblClass - ClassId,Class

tblSection- SectionId,Section



cmbClass将显示保存在tblClass中的类(值member-classId,display member-Class)

当我点击保存按钮时,它会将值成员cmbClass和txtbox值保存到tblSection。 datagridview dgvSection将显示SectionId,Class,Section。如何在dgvSection中显示该类?给我一个简单的方法来做到这一点。 thnk u。

这是我的代码 -



I have two table
tblClass - ClassId,Class
tblSection- SectionId,Section

cmbClass will display the class saved in tblClass (value member-classId, display member-Class)
when i will click on save button it will saved the value member cmbClass and a txtbox value to tblSection. And a datagridview dgvSection will show the SectionId,Class,Section . How to display the class in dgvSection? give me a easy way to do this. thnk u.
Here is my code -

' LOAD COMBOBOX ITEM FROM TBLCLASS
Public Sub loadclass()
        con.Open()
        LoadClassCmd = New SqlCommand("SELECT * FROM tblClass ", Con)
        Dim adpt As New SqlDataAdapter(LoadClassCmd)
        Dim dt As New DataTable
        adpt.Fill(dt)
        cmbClass.DataSource = dt
        cmbClass.DisplayMember = "Class"
        cmbClass.ValueMember = "ClassId"
        con.Close()
    End Sub

'LOAD DGV FROM TBLSECTION
Public Sub loaddgvSec()
        con.Open()
        Dim adpt As New SqlDataAdapter("SELECT * FROM tblSection", Con)
        Dim ds As New DataSet()
        adpt.Fill(ds)
        dgvSection.DataSource = ds.Tables(0)
        con.Close()
    End Sub

Private Sub frmClassMstr_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        
        loaddgvSec()
        loadclass()
    End Sub

Private Sub btnSecAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSecAdd.Click
'SAVE BUTTON
InsertSql = "INSERT INTO tblSection values('" & cmbClass.SelectedValue & "','"& txtSection.Text & "')"
        insertcmd = New SqlCommand(insertsql, con)  
            Try
                con.Open()

                insertcmd.ExecuteNonQuery()
                MsgBox("Section '" & txtSection.Text & "' in Class '" & cmbClass.Text & "' Inserted Successfully", MsgBoxStyle.Information)
                txtSection.Clear()
                cmbClass.Focus()

            Catch ex As Exception
                MsgBox(ex.Message())
            Finally
                con.Close()
        End Try
loaddgvSec()


End Sub

推荐答案

这是正确的方法吗?





Is This Correct way?


Public Sub loaddgvSec()
        con.Open()
        Dim adpt As New SqlDataAdapter("SELECT SectionId,(Select Class From tblClass WHERE tblSection.ClassId=tblClass.ClassId)AS Class,Section FROM tblSection", Con)
        Dim ds As New DataSet()
        adpt.Fill(ds)
        dgvSection.DataSource = ds.Tables(0)
        
        con.Close()
    End Sub


我看,现在更清楚,更改以下sql在你的代码中查询

I see, now it is clearer, change the following sql query in your code
Dim adpt As New SqlDataAdapter("SELECT * FROM tblSection", Con)



to


to

Dim adpt As New SqlDataAdapter("SELECT SectionId, Class, Section FROM tblSection inner join tblClass on ClassId = SectionId", Con)





那么你应该是获取带有Class和Section列的datagridview。



then you should be to get a datagridview with Class and Section columns.


这篇关于如何在表中保存Combobox.selectedvalue并在datagridview中显示Display成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆