我是这里的新手,关于清单框,我只需要一些帮助。我想从SQL Server数据库中获取值到checkedListBox。当我单击datagridview并单击视图按钮时,它将在另一种表单上显示数据,它对于ComboBox和TextBox正常工作,但是我不知道如何使用CheckedListBox进行操作。我有5个复选框
这是我将清单中的数据添加到sql server时的代码
Try
Dim sb As New System.Text.StringBuilder
For Each item In CheckedListBox1.CheckedItems
sb.Append(item)
sb.Append(" " + ",")
Next
Dim command As SqlClient.SqlCommand = con.CreateCommand()
command.CommandText = "insert into tblLegalAidCaseFile ([CCLA File Number], ApplicationDate, Name, Address, [Civil Status], Age, [Telephone Number], [Mobile Number], [Case Title], [Case Number], [Court/Agency], Venue, Branch, [Nature of Case], [Handling Lawyer], [Application Status], [Action Taken/Remarks], Refer, ApplicationRemarks) values (@cclafilenum, @date, @name, @address, @civil, @age, @telnum, @cpnum, @casetitle, @casenum, @court, @venue, @branch, @nature, @lawyer, @appstatus, @action, @refer, @denialreason)"
command.Parameters.AddWithValue("@cclafilenum", Me.CCLA_File_NumberLabel2.Text)
command.Parameters.Add("@date", SqlDbType.Date).Value = Me.ApplicationDateLabel2.Text
command.Parameters.AddWithValue("@name", Me.NameTextBox.Text)
command.Parameters.AddWithValue("@address", Me.AddressTextBox.Text)
command.Parameters.AddWithValue("@civil", Me.Civil_StatusComboBox.Text)
command.Parameters.AddWithValue("@age", Me.AgeComboBox.Text)
command.Parameters.AddWithValue("@telnum", Me.Telephone_NumberTextBox.Text)
command.Parameters.AddWithValue("@cpnum", Me.Mobile_NumberTextBox.Text)
command.Parameters.AddWithValue("@casetitle", Me.Case_TitleTextBox.Text)
command.Parameters.AddWithValue("@casenum", Me.Case_NumberTextBox.Text)
command.Parameters.AddWithValue("@court", Me.Court_AgencyComboBox.Text)
command.Parameters.AddWithValue("@venue", Me.VenueComboBox.Text)
command.Parameters.AddWithValue("@branch", Me.BranchComboBox.Text)
command.Parameters.AddWithValue("@nature", Me.Nature_of_CaseComboBox.Text)
command.Parameters.AddWithValue("@lawyer", Me.Handling_LawyerComboBox.Text)
command.Parameters.AddWithValue("@appstatus", Me.Application_StatusComboBox.Text)
command.Parameters.AddWithValue("@action", sb.ToString())
command.Parameters.AddWithValue("@refer", Me.ReferTextBox.Text)
command.Parameters.AddWithValue("@casestat", Me.Case_StatusTextBox.Text)
command.Parameters.AddWithValue("@denialreason", Me.ApplicationRemarksRichTextBox.Text)
command.ExecuteNonQuery()
MessageBox.Show("New Case File was Added in the System", "IBP Legal Aid Case Management System - Add New User", MessageBoxButtons.OK, MessageBoxIcon.Information)
clear()
Me.NameTextBox.Focus()
Catch ex As Exception
MessageBox.Show("An Error Occured: Please Try Again" & ex.Message.ToString(), "IBP Legal Aid Case Management System - Add New Case File", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
con.Close()
Private Sub viewbtn_Click(sender As Object, e As EventArgs) Handles viewbtn.Click
Dim id = TblLegalAidCaseFileDataGridView.CurrentRow.Cells(0).Value.ToString()
Dim command As New SqlCommand("SELECT * From TblLegalAidCaseFile WHERE [CCLA File Number]='" & id & "'", con)
Dim table As New DataTable()
Dim sqlAdapter As New SqlDataAdapter(command)
sqlAdapter.Fill(table)
frmUpdateCase.CCLA_File_NumberLabel2.Text = table.Rows(0)(1).ToString()
frmUpdateCase.ApplicationDateLabel2.Text = table.Rows(0)(2).ToLongDateString()
frmUpdateCase.NameTextBox.Text = table.Rows(0)(3).ToString()
frmUpdateCase.AddressTextBox.Text = table.Rows(0)(4).ToString()
frmUpdateCase.Civil_StatusComboBox.Text = table.Rows(0)(5).ToString()
frmUpdateCase.AgeComboBox.Text = table.Rows(0)(6).ToString()
frmUpdateCase.Telephone_NumberTextBox.Text = table.Rows(0)(7).ToString()
frmUpdateCase.Mobile_NumberTextBox.Text = table.Rows(0)(8).ToString()
frmUpdateCase.Case_TitleTextBox.Text = table.Rows(0)(9).ToString()
frmUpdateCase.Case_NumberTextBox.Text = table.Rows(0)(10).ToString()
frmUpdateCase.Court_AgencyComboBox.Text = table.Rows(0)(11).ToString()
frmUpdateCase.VenueComboBox.Text = table.Rows(0)(12).ToString()
frmUpdateCase.BranchComboBox.Text = table.Rows(0)(13).ToString()
frmUpdateCase.Nature_of_CaseComboBox.Text = table.Rows(0)(14).ToString()
frmUpdateCase.Application_StatusComboBox.Text = table.Rows(0)(16).ToString()
frmUpdateCase.Handling_LawyerComboBox.Text = table.Rows(0)(15).ToString()
frmUpdateCase.ReferTextBox.Text = table.Rows(0)(18).ToString()
frmUpdateCase.ApplicationRemarksRichTextBox.Text = table.Rows(0)(19).ToString
Me.Close()
frmUpdateCase.Show()
End Sub
保存的已选中项目是datagridview上的table.rows(0)(17),一旦单击数据,应在复选框中显示为已选中,谢谢您的帮助
我假定rows(0)(17)的值是一个逗号分隔的字符串。如果是这样,您可以按照以下步骤填充选中的列表框:
Dim items As List(Of String) = rows(0)(17).ToString.Replace(" ", "").Split({","}, StringSplitOptions.RemoveEmptyEntries).ToList
items.ForEach(Sub(a) CheckedListBox1.Items.Add(a, True))
如果您不喜欢LINQ方法,则可以这样做:
Dim items() As String = rows(0)(17).ToString.Replace(" ", "").Split({","}, StringSplitOptions.RemoveEmptyEntries)
For Each item As String In items
CheckedListBox1.Items.Add(item, True)
Next
编辑
我建议您在数据库中为actions创建一个新表,而不是在主表的@ action字段中将它们串联在一起的字符串。您需要在后端和前端设计之间进行区分。
同时,如果您的选中的列表框中包含许多项目,并且您只想检查从数据库(@action字段中)检索到的项目,则可以执行以下操作:
'Presuming that the row(0)(17) is the actions field.
Dim actions() As String = rows(0)(17).ToString.Replace(" ", "").Split(",")
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i, actions.Contains(CheckedListBox1.GetItemText(i)))
Next
祝你好运。>>