【问题标题】:Save row colour after closing form关闭表单后保存行颜色
【发布时间】: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


【解决方案1】:

以下代码应按照我的 cmets 中的描述为行着色。

Private Sub ColorRows()
  For Each row As DataGridViewRow In DataGridView1.Rows
    If (Not row.IsNewRow) And (row.Cells("Solved").Value IsNot DBNull.Value) Then
      row.DefaultCellStyle.BackColor = Color.PaleGreen
    End If
  Next
End Sub

您可以在数据加载到网格后立即以Load 事件的形式调用此代码。像……

….
DataGridView1.DataSource = table
ColorRows()

编辑...

经过一些测试,似乎当代码将“已解决”值设置为空字符串时,在Button4_Click 事件中...

DataGridView1.CurrentRow.Cells("Solved").Value = ""

这是设置一个默认的最小日期值,正如您在 xml 文件中记录的那样。

把这行代码改成...

DataGridView1.CurrentRow.Cells("Solved").Value = DBNull.Value

它应该可以按预期工作。

下面是我用来测试的完整代码。

Dim table As New DataTable("Table")
ReadOnly p As String = Path.Combine("D:\Test\XML\_test_100.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
  ColorRows()
End Sub

Private Sub ColorRows()
  For Each row As DataGridViewRow In DataGridView1.Rows
    If (Not row.IsNewRow) And (row.Cells("Solved").Value IsNot DBNull.Value) Then
      row.DefaultCellStyle.BackColor = Color.PaleGreen
    End If
  Next
End Sub

Private Sub btnWriteToXML_Click(sender As Object, e As EventArgs) Handles btnWriteToXML.Click
  table.WriteXml(p, XmlWriteMode.WriteSchema)
End Sub


Private Sub btnSolved_Click(sender As Object, e As EventArgs) Handles btnSolved.Click
  DataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.PaleGreen
  DataGridView1.CurrentRow.Cells("Solved").Value = DateTime.Now
  'DataGridView1.DataSource = table
  DataGridView1.ClearSelection()
End Sub

Private Sub btnRepairSolved_Click(sender As Object, e As EventArgs) Handles btnRepairedSolved.Click
  DataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.White
  DataGridView1.CurrentRow.Cells("Solved").Value = DBNull.Value
  'DataGridView1.DataSource = table
  DataGridView1.ClearSelection()
End Sub

【讨论】:

  • 您可能应该使用RowPrePaint / RowPostPaintCellFormat 事件来避免每次网格发生变化时重复此操作。即,使过程自动化。
  • 我讨厌不同意你@Jimi。当用户将光标移到网格上或用户滚动网格时,这些事件通常会被触发。当单元格值没有改变时,为什么要用这个“日期检查代码”来负担这些事件呢?仅当日期更改时才需要此更改,在这种情况下,仅通过单击按钮之一更改值。我猜我可能遗漏了什么。
  • @LM Rig... 代码如何将日期数据保存到数据库?我没有费心去创建一个数据库来测试这个。
  • 效果很好,除了那个“修复”按钮。当我设置 Solved 时,它是绿色的并且保持不变。然后我按下button4,它再次变白,日期时间被删除,但是当我重新打开表单时,它又变绿了。该怎么做才能解决这个问题? :-D 谢谢你的时间。
  • 它是 XML,而不是 DB。 "("C:\test.xml")" 从上面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
相关资源
最近更新 更多