问题描述
我的问题是
我有一个CheckedListBox
我正在其中加载
以
为基础的字段名称或说列"从ComboBox的选定索引中选择的表名已更改
Event.Fields的数量因每个选定的
而异Combobox中的表格,因此是CheckedListBox中的项目.
我只想要如何编写Select语句
获取与该字段关联的数据
在CheckedListBox中检查的内容.
或我可以将所有选中的字段分开吗
和逗号.
我从哪里开始感到困惑.
需要帮助.
My problem is that
I have got a CheckedListBox
in which I am loading the
Field names or say Columns on the basis of
selected table names from a ComboBox''s Selected Index Changed
Event.Number of Fields vary for Each selected
table From Combobox and hence the Items in CheckedListBox.
I just want, how to write a Select Statement
to fetch the data associated with the field
which are checked in the CheckedListBox.
or Can I Get all the checked fields seperated
with Comma .
I am getting Confused where to start.
Need help.
推荐答案
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Create a list of type string and initialise
Dim strings As List(Of String) = New List(Of String)
'Then for each string that is checked in the listbox
For Each s As String In CheckedListBox1.CheckedItems
'Add it to the list
strings.Add(s)
Next
'Now you have a list of all the selected strings
For Each s As String In strings
'I just show them in messagebox
MsgBox(s)
Next
End Sub
希望对您有帮助
Hope that helps
Public Sub getCheckedValues()
Dim sb As New StringBuilder
Dim i As Integer
For i = 0 To chkBox.Items.Count - 1
If chkBox.GetItemChecked(i) = True Then
sb.Append(chkBox.Items(i).ToString & ",")
End If
Next
Dim str As String = sb.ToString.Trim(",")
然后按照Rob的建议,使用动态存储过程来获取数据,然后将其加载到DataGridView
and then As Rob suggested,Used a Dynamic Stored Procedure to fetch the Data and then loaded them to The DataGridView
Dim con As New SqlConnection
Dim cmd As New SqlCommand
With con
.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=New1;Integrated Security=True;Pooling=False"
.Open()
End With
Dim s As String = cmbTabView.Text.ToString
Dim o As String = cmbColom.Text.ToString
Dim sCconnect = "Data Source=.\sqlexpress;Initial Catalog=New1;Integrated Security=True;Pooling=False"
Dim xSqlConnection As SqlConnection = New SqlConnection(sCconnect)
Dim xSqlCommand As SqlCommand = New SqlCommand("DynamicStoredProc", xSqlConnection)
xSqlCommand.CommandType = CommandType.StoredProcedure
xSqlCommand.Parameters.AddWithValue("@Select", str)
xSqlCommand.Parameters(0).SqlDbType = SqlDbType.VarChar
xSqlCommand.Parameters.AddWithValue("@Table", s)
xSqlCommand.Parameters(1).SqlDbType = SqlDbType.VarChar
xSqlCommand.Parameters.AddWithValue("@Order", o)
xSqlCommand.Parameters(2).SqlDbType = SqlDbType.VarChar
xSqlCommand.Connection.Open()
Dim xSqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(xSqlCommand)
Dim dt As New DataTable
xSqlDataAdapter.Fill(dt)
dgvData.DataSource = dt
End Sub
在显示按钮单击"上称为此函数".在这里,我还添加了按条款排序"以对数据进行排序.我使用的存储过程的结构是
called This Function on Show Button Click.Here I also added Order by Clause to Sort the Data.Structure of Stored Procedure what I used is as
ALTER PROCEDURE dbo.DynamicStoredProc
@Select varchar(500) = '''',
@Table varchar(500) = '''',
@Order varchar(500)=''''
AS
SET NOCOUNT ON;
DECLARE @Query varchar(500)
SET @Query = ''SELECT '' + @Select + '' FROM '' + @Table + '' ORDER BY '' + @Order
EXEC(@Query)
RETURN
仅此而已,是的,别忘了导入StringBuilder类所需的System.Text命名空间.
That''s all.And Yes Don''t Forget To import System.Text namespace which is needed for StringBuilder Class.
这篇关于如何从CheckBoxList中获取与Checked Field相关的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!