关闭表格后保存行颜色
Save row colour after closing form
我得到这个是为了在关闭表单时保存所有数据。
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
问题是,我的保存没有保存这个彩色行,当我再次打开表格时它是白色的。
任何的想法?我真的很陌生。
谢谢。
下面的代码应该按照我的评论中的描述为行着色。
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
我得到这个是为了在关闭表单时保存所有数据。
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
问题是,我的保存没有保存这个彩色行,当我再次打开表格时它是白色的。 任何的想法?我真的很陌生。
谢谢。
下面的代码应该按照我的评论中的描述为行着色。
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