根据单元格值更改datagridview行的颜色....... [英] To change the color of datagridview rows based on cells value.......

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

问题描述

问题:



专业人士和shifu可以查看我的代码吗?



只是非常奇怪我想根据我编码的单元格值更改行颜色,我认为必须是我的代码中的逻辑内容!



>但是当我强制获取单元格(0)时,它成功地改变了我发现的颜色是因为单元格(0)是整数值。为什么这些代码只接受整数值?



>如何强制它接受字符串?



>请指导我或任何解决方案?





将数据从表加载到DataGridView的代码

Question:

Can pros and shifu out there pls have a look through my codes?

just very weird i want to change the rows color based on the cell value that i had coded, i think must be something inlogic with my codes!

>but when i force to get the cell(0) it successfully changed the color i found out is because of cell(0) is integer value. why these codes only accept integer value?

>how to force it to accept string?

>please kindly guide me or any solutions?


Codes That Load Data From Table to the DataGridView

Public Sub loadalll1()
        Dim sqlq As String = "SELECT * FROM ICDTBL"
        Dim sqlcmd As New SqlCommand
        Dim sqladpt As New SqlDataAdapter
        Dim tbl As New DataTable

        With sqlcmd
            .CommandText = sqlq
            .Connection = connection
        End With

        With sqladpt
            .SelectCommand = sqlcmd
            .Fill(tbl)
        End With

        DataGridView1.Rows.Clear()
        For i = 0 To tbl.Rows.Count - 1
            With DataGridView1
                .Rows.Add(tbl.Rows(i)("IDNO"), tbl.Rows(i)("CreateBy"), tbl.Rows(i)("StartDate"), tbl.Rows(i)("CloseDate"), tbl.Rows(i)("StartTime"), tbl.Rows(i)("CloseTime"), tbl.Rows(i)("Supplier"), tbl.Rows(i)("ActualETA"), tbl.Rows(i)("OrderNum"), tbl.Rows(i)("Remark"), tbl.Rows(i)("Status"))
            End With
        Next
        connection.Close()
    End Sub







要更改我的DataGridView颜色的代码




Codes That to Change My DataGridView's color

Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)
       
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            If Me.DataGridView1.Rows(i).Cells(10).Value = "Pending" Then
                Me.DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Green
                Me.DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.White
            ElseIf Me.DataGridView1.Rows(i).Cells(10).Value = "Close" Then
                Me.DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
                Me.DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.White
            ElseIf Me.DataGridView1.Rows(i).Cells(10).Value = "In Progress" Then
                Me.DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Orange
                Me.DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.White
            End If
        Next
End Sub

推荐答案

问题:你的子句末尾没有句柄子句。

你必须用handle子句声明你的函数。

Handles子句定义绑定此事件的控件。应该是你的datagridview

Problem : no handles clause at the end of your sub.
You have to declare your function with handles clause .
Handles clause defines which control to bind this event. that should be your datagridview
'Update Your function as below.
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)  _ 
 Handles DataGridView1.CellFormatting
'where DataGridView1 is the name of your grid view
  For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            If Trim(Me.DataGridView1.Rows(i).Cells("Status").Value) = "Pending" Then
                Me.DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Green
                Me.DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.White
            ElseIf Trim(Me.DataGridView1.Rows(i).Cells("Status").Value) = "Close" Then
                Me.DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
                Me.DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.White
            ElseIf Trim(Me.DataGridView1.Rows(i).Cells("Status").Value) = "In Progress" Then
                Me.DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Orange
                Me.DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.White
            End If
        Next
End Sub





它不是整数或字符串的问题。

'修剪为删除空格而添加,如CHill60所述



Its not the problem of integer or of string.
'trim added for removing spaces as stated by CHill60


而不是此



Me.DataGridView1.Rows(i).Cells(10).Value



试试这个



Me.DataGridView1.Rows(i).Cells(10).Value.toString()
instead of this

Me.DataGridView1.Rows(i).Cells(10).Value

try this

Me.DataGridView1.Rows(i).Cells(10).Value.toString()


这篇关于根据单元格值更改datagridview行的颜色.......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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