2016-06-13 32 views
0

我目前在VB.NET express for desktop,2013中工作。我很難將SQL數據綁定到一個數組的循環上的一些datagridviews。我收到一個對象爲null的錯誤,因爲在直接投射線上它沒有拉動datagridview。我在一個選項卡控制工具上有多個datagridview,每個選項卡有一個datagridview。這裏是我的代碼:直接投給datagridview的空錯誤

try 
Dim array() As Integer = {"2", "3", "4", "7", "8", "10", "11", "12"} 

     For Each value As Integer In array 
      Dim RelativeDGV = DirectCast(Me.Controls("DataGridLine" & value), DataGridView) 
      Using conn1 As New SqlConnection(connstring) 
       conn1.Open() 
       Using comm1 As New SqlCommand("SELECT LineNumber FROM tableA where LineNumber = @LineNumber", conn1) 
        comm1.Parameters.AddWithValue("@LineNumber", value) 
        Dim dt As New DataTable 
        Dim sql As New SqlDataAdapter(comm1) 
        sql.Fill(dt) 
        RelativeDGV.DataSource = dt 
       End Using 
       conn1.Close() 
      End Using 
     Next 

    Catch ex As Exception 
     MsgBox(ex.ToString) 
    End Try 

的錯誤是在線

Dim RelativeDGV = DirectCast(Me.Controls("DataGridLine" & value), DataGridView) 

但零誤差這麼想的觸發器,直到

RelativeDGV.DataSource = dt 
+1

打開選項嚴格'「2」,「3」,「4」'不是整數 – Plutonix

+0

@Plutonix我打開選項嚴格它也改變了2,3,4,並且代碼仍然在竊聽我。 – Cheddar

+0

我無法重現該問題。假設你有'Option Infer On',那麼'RelativeDGV'將是'DataGridView'類型(否則你應該有'Dim RelativeDGV As DataGridView = DirectCast(.....)')。你確定DataGridLine2,DataGridLine3等都存在,屬於DataGridView類型,並且是表單控件集合的成員(不在「GroupBox」或其他容器中)? – Blackwood

回答

1

嘗試使用DataGridView名單如下:

Try 
    Dim array() As DataGridView = {DataGridLine2, DataGridLine3, DataGridLine4, DataGridLine7, DataGridLine8, DataGridLine10, DataGridLine11, DataGridLine12} 
    For Each RelativeDGV As DataGridView In array 
     Dim value As Integer = Regex.Replace(RelativeDGV.Name, "[^0-9]+", String.Empty) 
     'or like this 
     'Dim value As Integer = RelativeDGV.Name.Substring(12, RelativeDGV.Name.Length - 12) 
     Using conn1 As New SqlConnection(connstring) 
      conn1.Open() 
      Using comm1 As New SqlCommand("SELECT LineNumber FROM tableA where LineNumber = @LineNumber", conn1) 
       comm1.Parameters.AddWithValue("@LineNumber", value) 
       Dim dt As New DataTable 
       Dim sql As New SqlDataAdapter(comm1) 
       sql.Fill(dt) 
       RelativeDGV.DataSource = dt 
      End Using 
      conn1.Close() 
     End Using 
    Next 

Catch ex As Exception 
    MsgBox(ex.ToString) 
End Try 
+0

給我一秒鐘,我會試試這個。我只需要將第二個數組放入參數部分中的「值」變量,該參數部分必須從2更改爲3,然後依此類推。 – Cheddar

+0

這是工作,我有一些錯誤,但它的堅實。 – Cheddar

+0

@切達很高興聽到!你可以像DataGridView那樣從「DataGridView」的名稱中提取值。 –

1

如果各種D GV控件在其他選項卡上,它們不會在Me.Controls中。而不是將它們釣魚並施放它們,你可以迭代它們的數組,因爲你知道這個名字。你也不需要爲每個創建的每個新的連接也不重複數據表:

Dim dgvCtrls As DataGridView() = {DataGridLine2, DataGridLine3, DataGridLine4} 

Using conn1 As New SqlConnection(connstring) 
    conn1.Open() 
    Using comm1 As New SqlCommand("SELECT LineNumber FROM...", conn1) 
     ' ... 
     dt.Load(comm1.ExecuteReader()) 
    End Using 

    conn1.Close() 
End Using 

For Each dgv In dgvCtrls 
    dgv.DataSource = dt 
Next 

你只需要8個相同的數據表,如果你不想每個網格自動反映在他人所做的更改。爲此,使用數據集相同的連接上創建表:

Dim SQL = "..." 
Dim dgvCtrls As DataGridView() = {dgv5, dgv2, dgv3,...} 
Dim ds = New DataSet 

Using dbcon As New SqlConnection(SQLConnStr) 
    Using cmd As New SqlCommand(SQL, dbcon) 
     dbcon.Open() 
     For n As Int32 = 0 To dgvCtrls.Count - 1 
      ds.Load(cmd.ExecuteReader, LoadOption.OverwriteChanges, dgvCtrls(n).Name) 
     Next 
    End Using 
End Using 

For Each dgv In dgvCtrls 
    dgv.DataSource = ds.Tables(dgv.Name) 
Next