2017-05-13 58 views
-1

我想將DataGridView的內容存儲在數組中 ,反之亦然,我想將數組讀入Datagridview。如何將datagridview作爲數組存儲在mysql表的單元格中

我發現這個班的淨

Public Class Loads 
    Private L_nr As String 
    Private L_plz As String 
    Private L_km As String 

    Public Sub New(ByVal nr, ByVal plz, ByVal km) 
     L_nr = nr 
     L_plz = plz 
     L_km = km 
    End Sub 
    Public Property nr() As String 
     Get 
      Return L_nr 
     End Get 
     Set(ByVal Value As String) 
      L_nr = Value 
     End Set 
    End Property 
    Public Property plz() As String 
     Get 
      Return L_plz 
     End Get 
     Set(ByVal Value As String) 
      L_plz = Value 
     End Set 
    End Property 
    Public Property km() As String 
     Get 
      Return L_km 
     End Get 
     Set(ByVal Value As String) 
      L_km = Value 
     End Set 
    End Property 

End Class 

我XAN使用這樣

Private Sub DataGridViewLoads1_Click(sender As Object, e As EventArgs)  Handles DataGridViewLoads1.Click 

    Dim al As New Collections.ArrayList() 
    al.Add(New Loads(123, 234, 1971)) 
    DataGridViewLoads1.DataSource = al 

End Sub 

但它不是我想要

回答

0

您可以使用類實例的集合,用於顯示什麼並編輯數據DataGridView

Public Class Person 
    Public Property Id As Integer 
    Public Property Name As String 
End Class 

創建收集並顯示在DataGridView

Dim persons = new List(Of Person) From 
{ 
    New Person { .Id = 1, .Name = "One" }, 
    New Person { .Id = 2, .Name = "Two" }, 
    New Person { .Id = 3, .Name = "Three" } 
} 

yourDataGridView.DataSource = persons 

當您想要保存從personsDataGridView使用同一實例的所有數據,或從.DataSource

Dim dataToSave = DirectCast(yourDataGridView.DataSource, List(Of Person)) 

投它,然後在json格式的序列數據串(隨意使用任何其他格式)

Dim serializedData AS String = Newtonsoft.Json.JsonConvert.SerializeObject(dataToSave) 

然後將該字符串保存在數據庫中。

爲了從數據庫中顯示數據:

// retrieve value from database 
Dim serializedData As String = GetFromDatabaseYourFunction() 
Dim data = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of Person))(serializedData) 
yourDataGridView.DataSource = data 
相關問題