怎么ComboBox的下拉值来查询其他表
有三个表,第一个表叫章节表,有个章节名的值;第二个表叫题型表,有个题型名的值;第三个表叫试题表,有章节名和题型名两个值把章节表的章节名 和题型表的题型名 分别连接到 ComboBox1 ComboBox2 通过下拉想要的选项,添加Button1 把所选的ComboBox1 ComboBox2 的值查询试题表的章节名和题型表,并把返回的数值返回给连接了试题表的DataGridView1
现在怎么实现 ComboBox1 ComboBox2 这两个值可以下拉查询 --------------------编程问答-------------------- 没有没有人懂吗?! --------------------编程问答-------------------- 用动态sql语句 --------------------编程问答-------------------- 可是 ComboBox1 下拉出来的值找不到
不知道该怎么写 --------------------编程问答-------------------- FormLoad里面,就查询章节表,把章节名的值加入ComboBox1里,
然后ComboBox1的SelectedValueChanged事件里,根据选择的章节名,查询题型表,将对应的题型名加
入+ComBox2里。
然后根据章节名,题型名,用Button2把试题查询的DataTable绑定到DataGridView1上 --------------------编程问答--------------------
.selectedvalue --------------------编程问答-------------------- 现在遇到的问题是 只要一修改数据的话 所有的 combobox 控件全部连接不到数据
全部是空的 没有下拉菜单
Imports System.Data
Imports System.Data.SqlClient
Public Class Form5
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Me.Close()
Form1.Show()
End Sub
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: 这行代码将数据加载到表“试题库DataSet3.题型表”中。您可以根据需要移动或移除它。
Me.题型表TableAdapter1.Fill(Me.试题库DataSet3.题型表)
'TODO: 这行代码将数据加载到表“试题库DataSet.试题表”中。您可以根据需要移动或移除它。
Me.试题表TableAdapter.Fill(Me.试题库DataSet.试题表)
'TODO: 这行代码将数据加载到表“试题库DataSet.题型表”中。您可以根据需要移动或移除它。
Me.题型表TableAdapter.Fill(Me.试题库DataSet.题型表)
'TODO: 这行代码将数据加载到表“试题库DataSet.章节表”中。您可以根据需要移动或移除它。
Me.章节表TableAdapter.Fill(Me.试题库DataSet.章节表)
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim row As Int32 = Me.DataGridView1.CurrentCell.OwningRow.Index
Dim str As String = Me.DataGridView1.Rows(row).Cells(1).Value.ToString()
'str是你的ID字段
If e.ColumnIndex = 0 Then
'如果用户点中了第一列,就弹出删除对话框
Dim i As Integer = MsgBox("确定删除数据?", MsgBoxStyle.OkCancel)
If i = 1 Then
'在这里添加
'这里调用删除的代码 你在这里写就行了
'删除可以用了!!!
Dim strconn As New SqlConnection("SERVER=127.0.0.1;uid="";pwd="";database=试题库")
strconn.Open()
Dim strdel As String = "Delete from [试题表] where ID=" & str
Dim cmd As New SqlCommand(strdel, strconn)
cmd.ExecuteNonQuery()
Dim 试题表TableAdapter As New 试题库DataSetTableAdapters.试题表TableAdapter
'Me.试题库DataSet.试题表.Clear()
Me.试题库DataSet.Reset()
Me.试题库DataSet.EnforceConstraints = False
试题表TableAdapter.Fill(Me.试题库DataSet.试题表)
MsgBox(" 删除成功!")
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or ComboBox3.Text = "" Or ComboBox4.Text = "" Or ComboBox5.Text = "" Then
MsgBox("请填写完整!")
Else
Dim strconn As String
strconn = "SERVER=127.0.0.1;uid="";pwd="";database=试题库"
Dim objconn As New SqlConnection(strconn)
Dim objadap As New SqlDataAdapter()
Dim objdset As New DataSet
objconn.Open()
Dim th As New SqlParameter("@th", SqlDbType.Char, 10)
Dim tx As New SqlParameter("@tx", SqlDbType.Char, 10)
Dim zj As New SqlParameter("@zj", SqlDbType.Char, 10)
Dim nd As New SqlParameter("@nd", SqlDbType.VarChar, 10)
Dim fz As New SqlParameter("@fz", SqlDbType.Char, 10)
Dim tm As New SqlParameter("@tm", SqlDbType.VarChar, 3500)
Dim da As New SqlParameter("@da", SqlDbType.VarChar, 3500)
Dim strsqlzd As String = "insert into 试题表(题号,题型,章节,难度,分值,题目,答案)" & "values(@th,@tx,@zj,@nd,@fz,@tm,@da)"
Dim objcmd As New SqlCommand(strsqlzd, objconn)
objcmd.Parameters.Add(th)
objcmd.Parameters.Add(tx)
objcmd.Parameters.Add(zj)
objcmd.Parameters.Add(nd)
objcmd.Parameters.Add(fz)
objcmd.Parameters.Add(tm)
objcmd.Parameters.Add(da)
th.Value = TextBox1.Text
fz.Value = TextBox2.Text
tm.Value = TextBox3.Text
da.Value = TextBox4.Text
tx.Value = ComboBox3.Text
zj.Value = ComboBox4.Text
nd.Value = ComboBox5.Text
objcmd.ExecuteNonQuery()
objconn.Close()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Dim 试题表TableAdapter As New 试题库DataSetTableAdapters.试题表TableAdapter
'Me.试题库DataSet.试题表.Clear()
Me.试题库DataSet.Reset()
Me.试题库DataSet.EnforceConstraints = False
试题表TableAdapter.Fill(Me.试题库DataSet.试题表)
MsgBox(" 添加成功!")
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim strconn As String
Dim sqldset As New 试题库DataSet()
strconn = "SERVER=127.0.0.1;uid="";pwd="";database=试题库"
Dim sqlconn As New SqlConnection(strconn)
Dim sqladap As New SqlDataAdapter()
Dim strupd As String
sqlconn.Open()
If TextBox1.Text > "" And TextBox2.Text > "" And TextBox3.Text > "" And TextBox4.Text > "" And TextBox5.Text > "" Then
strupd = "update 试题表 set 题号 ='" & TextBox1.Text & "' , 分值 ='" & TextBox2.Text & "', 题目='" & TextBox3.Text & "', 答案='" & TextBox4.Text & "', 题型='" & ComboBox3.Text & "',章节='" & ComboBox4.Text & "',难度='" & ComboBox5.Text & "'where ID ='" & TextBox5.Text & "'"
Dim sqlcmd As New SqlCommand(strupd, sqlconn)
sqlcmd.ExecuteNonQuery()
MsgBox("修改成功!", MsgBoxStyle.OkOnly, "提示")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox1.Focus()
Dim 试题表TableAdapter As New 试题库DataSetTableAdapters.试题表TableAdapter
' Me.试题库DataSet.试题表.Clear()
Me.试题库DataSet.Reset()
试题表TableAdapter.Fill(Me.试题库DataSet.试题表)
Else
MsgBox("请填写完整!")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strsql As String
Dim strconn As String
Dim sqldset As New 试题库DataSet()
strconn = "SERVER=127.0.0.1;uid="";pwd="";database=试题库"
Dim sqlconn As New SqlConnection(strconn)
Dim sqladap As New SqlDataAdapter()
If ComboBox1.Text <> "" And ComboBox2.Text = "" Then
strsql = "select * from 试题表 where 章节 ='" & ComboBox1.Text & "'"
ElseIf ComboBox1.Text = "" And ComboBox2.Text <> "" Then
strsql = "select * from 试题表 where 题型 ='" & ComboBox2.Text & "'"
Else
strsql = "select * from 试题表 where 章节 ='" & ComboBox1.Text & "'and 题型='" & ComboBox2.Text & "'"
End If
sqlconn.Open()
sqladap.SelectCommand = New SqlCommand(strsql, sqlconn)
sqlconn.Close()
sqldset.Clear()
sqladap.Fill(sqldset, "试题表")
DataGridView1.DataSource = sqldset.Tables("试题表")
End Sub
--------------------编程问答-------------------- 这里没看到你的combobox是怎么赋值的` --------------------编程问答-------------------- combobox 在界面的时候已经连接到数据库了
--------------------编程问答-------------------- 应该需要重新关联一次你的combobox的数据源 --------------------编程问答-------------------- 谢谢大家了
我自己做出来了
就是 'Me.试题库DataSet.试题表.Clear()
Me.试题库DataSet.Reset()
Me.试题库DataSet.EnforceConstraints = False
试题表TableAdapter.Fill(Me.试题库DataSet.试题表)
把数据库刷新的注释掉 不让它刷新,数据有填充进去
'Me.试题库DataSet.试题表.Clear()
'Me.试题库DataSet.Reset()
Me.试题库DataSet.EnforceConstraints = False
试题表TableAdapter.Fill(Me.试题库DataSet.试题表) --------------------编程问答-------------------- 呵呵~~~加油
补充:.NET技术 , VB.NET