Click here to Skip to main content
14,937,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
VB
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



Codes That to Change My DataGridView's color
VB
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
Posted
Comments
yourfriendaks 14-Jun-13 6:12am
   
Can U Clarifiy It. What Is The means Of Integer Here With Which Sense
and u wand to change particular cells
or u want to change particular row based on cell value
donaldliaw87 14-Jun-13 7:46am
   
sorry unclear explanation~! actually i want to change row colour based on cell value, but i fail to achieve, therefore i try to change my cell(10)'str' to cell(0)'int' to see whether it change or not and my datarow could change the colour with cell(0) with 'int' value, but when i change back to cell(10) with str value it unable to change my datarow colour~!
CHill60 14-Jun-13 6:31am
   
Your code appears to work ok for me - are you sure that tbl.Rows(i)("Status") contains the data you are expecting? For example it is spelled correctly and not in capitals
Surendra Adhikari SA 14-Jun-13 6:52am
   
agree @CHill60.
this is working code (logic is fine) .just debug and watch for data.
donaldliaw87 14-Jun-13 7:51am
   
yes! i checked for few times edy, in the field "status" only content 3 types of char(20) value (Pending,In Progress, & Close) but weird can't change at all....
CHill60 14-Jun-13 7:56am
   
Ah ... try Trim(Me.DataGridView1.Rows(i).Cells(10).Value) ... might be some spaces in there
donaldliaw87 14-Jun-13 8:03am
   
will try soon.... thx shifu.....! Got question to ask!!, e.g. if inserting a line of words into a data field char(100), will that line of words bit different with what we key in? will it be? sorry ask this kind of question, i'm new to programming!!!
Surendra Adhikari SA 14-Jun-13 8:41am
   
Your Sub does not have Handles clause, I have updated solution ,
without handles clause you have to call that sub, it does not fired on events of control.
try and enjoy.
donaldliaw87 16-Jun-13 21:14pm
   
Did you mean my load event sub?? ofcourse i got handles clause it in my formload just only i did not post up my handle clause because of unnecessary, my main problem was on changing color!!!

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
VB
'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


Its not the problem of integer or of string.
'trim added for removing spaces as stated by CHill60
   
v4
Comments
CHill60 14-Jun-13 8:58am
   
Good spot!! My +5 ... I just pasted the contents of his sub into my event handler which of course is why it worked for me!
donaldliaw87 16-Jun-13 21:07pm
   
CHill60, you mean you pasted which sub? the load event sub or the cellformatting sub?? i tried the solution you all gave me (trim().tostring.cells(""), but still can't get what i want T.T"!!!
Surendra Adhikari SA 17-Jun-13 0:51am
   
did you copy and paste my solution ? remove DataGridView1_CellFormatting sub (whole) and copy above code , didn't it worked for you ?? ... <<amazed>> its running fine for me (i have tested your code in my mechine)
donaldliaw87 17-Jun-13 1:57am
   
i got this error="Column named Status cannot be found.
Parameter name: columnName" how to solve this?? pls help me!!
donaldliaw87 17-Jun-13 2:09am
   
i found out what's wrong edy!!! i did not change my column name i only changed the text header that's why i can't get my requirement ==" sorry was my mistake haiz............. sorry!
Surendra Adhikari SA 17-Jun-13 5:00am
   
its ok man.
instead of this

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

try this

Me.DataGridView1.Rows(i).Cells(10).Value.toString()
   
v3
Comments
donaldliaw87 16-Jun-13 21:11pm
   
hi yourfriendaks, the add of (.tostring) can't help me.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900