下面是我的代码,我一直在尝试为单元格着色,但不确定当我的网格源是数据库时如何使用“样式”属性。我是新手,所以很难开始。
一些网站或指针会有很大帮助。
我希望能够为一些背景单元格着色或为某些行或特定列着色......基本上所有与颜色相关的东西。我怎样才能用我当前的代码片段做到这一点?另外,我可以了解更多信息的链接将不胜感激。
LarsTech 我正在尝试将您添加到聊天中,但我没有足够的代表,因此我想我无法与您联系。
Imports System.Data.SqlClient
Imports System.Collections.Generic
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString As String = "data source=SQst; database=MVar2; User ID=Wepp; Password=2010Live; Integrated Security=false;"
Dim sql As String = "SELECT * FROM Prer"
Dim connection As New SqlConnection(connectionString)
Dim dataadapter As New SqlDataAdapter(sql, connection)
Dim ds As New DataSet()
connection.Open()
dataadapter.Fill(ds, "Authors_table")
connection.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Authors_table"
**DataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.PaleGreen
DataGridView1.Rows[3].Cells[1].Style.BackColor = Color.Red**
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call PrintDGV.Print_DataGridView(DataGridView1)
End Sub
End Class
我得到的错误:
Error 1 Property access must assign to the property or use its value.
Error 2 Identifier expected.
Error 3 Property access must assign to the property or use its value.
Error 4 Identifier expected.
我试过:DataGridView1.Rows(0).Cell(0).Style.BackColor = Color.Red
我得到了 1 个错误:
Error 1 'Cell' is not a member of 'System.Windows.Forms.DataGridViewRow'.
编辑:在网上查看更多内容后,我使用以下代码为选定的单元格着色:
DataGridView1.Item(4, 5).Style.BackColor = Color.Red
然而,这不会为行或列着色,所以我仍然希望让这些东西发挥作用。