【发布时间】:2021-05-08 13:29:36
【问题描述】:
我得到这个是为了在关闭表单时保存所有数据。
Public Class Form1
Dim table As New DataTable("Table")
ReadOnly p As String = Path.Combine("C:\test.xml")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
If Not File.Exists(p) Then
table.Columns.Add("Company", Type.GetType("System.String"))
table.Columns.Add("Date", Type.GetType("System.DateTime"))
table.Columns.Add("Code", Type.GetType("System.String"))
table.Columns.Add("Position", Type.GetType("System.String"))
table.Columns.Add("Note", Type.GetType("System.String"))
table.Columns.Add("Solved", Type.GetType("System.DateTime"))
Else
table.ReadXml(p)
End If
DataGridView1.DataSource = table
End Sub
我用它来标记已解决的行:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
DataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.PaleGreen
DataGridView1.CurrentRow.Cells("Solved").Value = DateTime.Now
DataGridView1.DataSource = table
DataGridView1.ClearSelection()
End Sub
这对于“修复已解决的行”按钮:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
DataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.White
DataGridView1.CurrentRow.Cells("Solved").Value = ""
DataGridView1.DataSource = table
DataGridView1.ClearSelection()
End Sub
问题是,我的保存并没有保存这个彩色行,当我再次打开表单时它是白色的。 任何的想法?我真的很陌生。
谢谢。
【问题讨论】:
-
是什么让您相信从网格中保存数据“也”可以保存行颜色?我没有看到任何保存行颜色的代码。什么标准决定了每行的颜色应该是什么?在大多数情况下,对行着色是在读取数据“之后”完成的。但是,您也可以“保存”这种颜色,这取决于决定颜色的因素。如果行中的值指示行应该是“什么”颜色,则不需要保存颜色,但是,如果需要,您可以保存颜色。重点是......简单地“保存”网格中的数据不会保存行颜色。
-
我只是不知道该怎么做:-)。哪一行的颜色决定了我点击按钮(上面代码中的 3 和 4)。因为我需要手动确认这一行是否已解决。当我单击已解决时 - 它变为绿色并添加当前日期+时间。当我想“重置”时,我使用按钮 4 删除日期和时间并将其重新着色为白色。
-
如果数据中的“Solved”
DateTime字段用于判断颜色。然后“在”数据加载到网格中之后,遍历行并检查每个“已解决”字段。如果Solved字段有一个日期,并且该日期在当前日期“之前”,则为该行着色。我会假设“已解决”也可能是null或空的……在这种情况下,这意味着……不要为该行着色。 -
你能展示将数据写回 XML 文件的代码吗?
标签: vb.net winforms datagridview