2014-02-16 128 views
1

我需要填充DataGridview多選擇 我想填充dataGridview通過此代碼,它的工作,但是當我鍵入 1,2,3它獲得正確的值,但是當我鍵入700它得到7不是700 當我鍵入1,2,3,10它得到1,2,3 所以我怎麼可以輸入1,2,3,10,700以及與這些數據填充的DataGridView 感謝通過多值填充DataGridView

Imports System.Data 
Imports System.Data.OleDb 
Imports System.Text 
Public Class Form1 
    Public OLECON As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\SCHEDULES_DB.accdb;Jet OLEDB:Database [email protected];") 
    Dim dt As New DataTable 
    Dim da As New OleDbDataAdapter 
    Dim cmd As New OleDbCommand 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

     Dim sb As New StringBuilder() 

     For Each fieldId As String In TextBox1.Text 

      sb.Append("'").Append(fieldId).Append("',") 
     Next 

     Dim fieldList As String = sb.ToString().TrimEnd(","c) 

     Dim SQLStr As String = "Select * from Tbl_Employee where EmpID in (" & fieldList & ") order by EmpID" 

     dt.Clear() 
     cmd.Parameters.Clear() 

     cmd.CommandText = SQLStr 

     cmd.Connection = OLECON 
     da.SelectCommand = cmd 

     da.Fill(dt) 
     DataGridView1.DataSource = dt 

    End Sub 

End Class 

回答

0

你正在分割每個角色的文本,這不是我認爲你想要做的。它看起來像你正試圖把周圍的逗號分隔字符串中的每個項目單引號,所以它可能應該是這個樣子,你用逗號分割的字符串的內容:

Dim items As New List(Of String) 
For Each s As String In TextBox1.Text.Split(",") 
    items.Add("'" & s & "'") 
Next 
Dim fieldList As String = String.Join(",", items.ToArray) 

的EmpID聲音並且看起來像數字,在這種情況下,單引號不是必需的。

+0

100%右許多謝謝 –