问题描述
这是否可以获取数据网格的每一行的所有单元格,然后将其传递给List
如下列:
FirstName LastName年龄性别
Ron Miler 23男
Steve Palioc 20男
Sarah Carter 18女
我想将List保存到my.setting,这样当我关闭我的winForm时,它将保存datagrid的最新项目,当我开始打开我的winForm时它只会加载数据
datagrid可能只有40行..
感谢提前
Is this possible to get all the cell every row of a datagrid then pass it to a List
like columns of:
FirstName LastName Age Gender
Ron Miler 23 Male
Steve Palioc 20 Male
Sarah Carter 18 Female
I want to save the List to my.setting so that when ever I close my winForm it will save the latest item of the datagrid, and when I start Open my winForm it will just load the data
datagrid may only have 40 rows..
Thanks in Advance
推荐答案
Public Class Form1
Private clsDS As New DataSet 'Class level variable
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'When loading the form, check to see if the setting is there.
If My.Settings.gblSettingDataSet IsNot Nothing Then
clsDS = My.Settings.gblSettingDataSet
End If
If clsDS.Tables.Count > 0 Then
DataGridView1.DataSource = clsDS.Tables(0)
End If
End Sub
Private Sub btnInitializeGridFirstTime_Click(sender As System.Object, e As System.EventArgs) Handles btnInitializeGridFirstTime.Click
'The first time the app is run, it doesn't have any schema to allow users to add rows in the grid
'This method is just so my test app will work.
If clsDS.Tables.Count = 0 Then
Dim dt As New DataTable
dt.Columns.Add("A")
dt.Columns.Add("B")
dt.Columns.Add("C")
clsDS.Tables.Add(dt)
End If
DataGridView1.DataSource = clsDS.Tables(0)
End Sub
Private Sub btnSaveGridToSettings_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveGridToSettings.Click
My.Settings.gblSettingDataSet = clsDS
My.Settings.Save()
End Sub
End Class
这样可行,但是再次......如果您的网格我不推荐这个最终可能会有大量数据,或者如果您正在构建一个需要专业的程序。存储大量数据是数据库的用途。
This works, but again...I don't recommend this if your grid could end up with a lot of data or if you are building a program that needs to be professional. Storing large amounts of data is what databases are for.
这篇关于将datagridview行传递给列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!