如何设置datagridview单元格颜色 [英] how to set the datagridview cell color

查看:126
本文介绍了如何设置datagridview单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hallo,

我保存了颜色名称tu mysql数据库,在保存颜色名称期间这些都没有问题。但当我检索颜色名称并将其显示到datagridview时,我的代码无法运行。我只想在datagridview单元格上显示颜色,具体取决于该单元格中的颜色名称。



hallo,
i have saving the color name tu mysql database and during saving the color name these is no problem. but when i retrieve the color name back and display it into datagridview, my code is not functioning. i just want to display a color on a datagridview cell depend on the color name in that cell.

Private Sub data_load()
    Dim dt As New DataTable
    Dim da As New OdbcDataAdapter
    Dim strSQL As String
    If Not ConnectDB(Conn, g_Conn) Then Exit Sub
    strSQL = "SELECT RowNo,ShiftID,ShiftName,StartIn,ActualIn,EndIn,"
    strSQL = strSQL & "ActualOut,EndOut,Color FROM tams_shift"
    strSQL = strSQL & " ORDER BY CAST(RowNo AS UNSIGNED) ASC"
    da.SelectCommand = New OdbcCommand(strSQL, Conn)
    dt = New DataTable
    da.Fill(dt)
    For i As Integer = 0 To dt.Rows.Count - 1
        Dim col As Color = Color.FromArgb(dt.Rows(i).Item("Color"))
        With dgvShift
            .DataSource = dt
            .Columns(0).Width = 60
            .Columns(1).Width = 60
            .Columns(2).Width = 140
            .Columns(3).Width = 60
            .Columns(4).Width = 60
            .Columns(5).Width = 60
            .Columns(6).Width = 60
            .Columns(7).Width = 60
           .Rows(i).Cells("Color").Style.BackColor = col
        End With
    Next
End Sub



i希望有人可以帮助我。

tq非常很多


i hope that someone can help me out.
tq very much

推荐答案

WinForms:

=======

见:DataGridViewCellStyle.BackColor [ ^ ]并使用以下方法从数据表中获取颜色: Color.FromName方法 [ ^ ]



示例:

更改单个DataGridView基于列值的行颜色 [< a href =http://www.codeproject.com/Tips/236125/Change-individual havenGridView-row-colors-based-ontarget =_ blanktitle =New Window> ^ ]

http: //www.vbforums.com/showthread.php?465358-RESOLVED-Change havenGridView-cell-color-based-on-other-columns [ ^ ]

http://social.msdn.microsoft.com / forums / zh-CN / winforms / thread / b60b9a19-52e6-4989-96b5-c02c0329bf9d [ ^ ]
WinForms:
=======
See this: DataGridViewCellStyle.BackColor[^] and get color from your datatable using: Color.FromName method[^]

Examples:
Change individual DataGridView row colors based on column value[^]
http://www.vbforums.com/showthread.php?465358-RESOLVED-Change-DataGridView-cell-color-based-on-other-columns[^]
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/b60b9a19-52e6-4989-96b5-c02c0329bf9d[^]


感谢您提示,

它的工作......

i就这样做了





私人子数据_载()

Dim dt As New DataTable

Dim da As New OdbcDataAdapter

Dim strSQL As String

如果不是ConnectDB(Conn,g_Conn)则退出Sub

strSQL =SELECT RowNo,ShiftID,ShiftName,StartIn,ActualIn,EndIn,

strSQL = strSQL& ActualOut,EndOut,Color FROM tams_shift

strSQL = strSQL& 按照CAST命令(RowNo AS UNSIGNED)ASC

da.SelectCommand = New OdbcCommand(strSQL,Conn)

dt = New DataTable

da.Fill(dt)

使用dgvShift

.DataSource = dt

.Columns(0).Width = 60

.Columns(1).Width = 60

.Columns(2).Width = 140

.Columns(3).Width = 60

.Columns(4).Width = 60

.Columns(5).Width = 60

.Columns(6).Width = 60
.Columns(7).Width = 60

''。列(8)。宽度= 60

结束

对于每个dgv As DataGridViewRow在dgvShift.Rows

dgv.Cells(Color)。Style.BackColor = Color.FromArgb(dgv.Cells(Color)。Value)

dgv.Cells(Color)。Tag = dgv.Cells(Color)。Value

dgv.Cells(Color)。Value =

下一页

结束次级







和i加载它在绘画功能中。



thanks for the tips,
it''s working...
i just done it like this


Private Sub data_load()
Dim dt As New DataTable
Dim da As New OdbcDataAdapter
Dim strSQL As String
If Not ConnectDB(Conn, g_Conn) Then Exit Sub
strSQL = "SELECT RowNo,ShiftID,ShiftName,StartIn,ActualIn,EndIn,"
strSQL = strSQL & "ActualOut,EndOut,Color FROM tams_shift"
strSQL = strSQL & " ORDER BY CAST(RowNo AS UNSIGNED) ASC"
da.SelectCommand = New OdbcCommand(strSQL, Conn)
dt = New DataTable
da.Fill(dt)
With dgvShift
.DataSource = dt
.Columns(0).Width = 60
.Columns(1).Width = 60
.Columns(2).Width = 140
.Columns(3).Width = 60
.Columns(4).Width = 60
.Columns(5).Width = 60
.Columns(6).Width = 60
.Columns(7).Width = 60
''.Columns(8).Width = 60
End With
For Each dgv As DataGridViewRow In dgvShift.Rows
dgv.Cells("Color").Style.BackColor = Color.FromArgb(dgv.Cells("Color").Value)
dgv.Cells("Color").Tag = dgv.Cells("Color").Value
dgv.Cells("Color").Value = ""
Next
End Sub



and i load it inside the paint function.

Private Sub frmShift_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
      data_load()
  End Sub









非常感谢





thanks very much


这篇关于如何设置datagridview单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆