none
How to avoid that the last line of Datagridview is not supported during Cellformating event. RRS feed

  • Question

  • Hi,

    I am using this Code to changing the color of a row in Datagridview if date and time match Now.

    Private Sub Dgw_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles Dgw.CellFormatting
         
                If Not Dgw.Rows(e.RowIndex).IsNewRow Then
                    If e.ColumnIndex = Dgw.Columns("DateDeCouvaison").Index Then
                        e.CellStyle.Format = "d"
                        Dim TheDate As Date = CDate(e.Value)
                        Console.WriteLine(TheDate.ToShortDateString)
                        If TheDate.ToShortDateString = Now.AddDays(-2).ToShortDateString Then
                            e.CellStyle.BackColor = Color.SpringGreen
                            e.CellStyle.ForeColor = Color.Red
                        End If
                   
                        If TheDate.ToShortDateString = Now.AddDays(-2).ToShortDateString Then
                            e.CellStyle.Font =
                                New System.Drawing.Font(
                                    "Segoe UI Semibold",
                                    9.0!,
                                    System.Drawing.FontStyle.Bold,
                                    System.Drawing.GraphicsUnit.Point,
                                    CType(0, Byte)
                                )
                        End If
                    End If
                 End If
        End Sub

     

    The code is running very Well whithout the last line which is a Total line,

    Is there a way to avoid that this line  To be  not supported during Cellformating event.

    as

    Thank you Very Much and Best Regards.



    • Edited by Bajtitou Sunday, June 16, 2019 9:23 AM
    Sunday, June 16, 2019 9:22 AM

Answers

  • Yes the code works Very Well, I want to exclude the last line of Cell Formating.

    Thank you very Much & Best regards.

    Hi

    The replacement line of code I suggested may be what you want then.


    Regards Les, Livingston, Scotland

    • Marked as answer by Bajtitou Sunday, June 16, 2019 1:44 PM
    Sunday, June 16, 2019 1:30 PM

All replies

  • Hi,
    check the last row. If the last row is a new row the cell formatting will be excluded:

    If Not Dgw.Rows(e.RowIndex).IsNewRow Then
    


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Sunday, June 16, 2019 10:16 AM
  • Hi,
    check the last row. If the last row is a new row the cell formatting will be excluded:

    If Not Dgw.Rows(e.RowIndex).IsNewRow Then

    Hi, 

    How To Do it??

    Thank you Very Much,

    Best Regards.

    Sunday, June 16, 2019 12:08 PM
  • Hi,
    check the last row. If the last row is a new row the cell formatting will be excluded:

    If Not Dgw.Rows(e.RowIndex).IsNewRow Then

    Hi, 

    How To Do it??

    Thank you Very Much,

    Best Regards.

    Hi

    I have tried your code and it seems to work OK here. It detects and changes the BackColor and Font of the correct cell(s) and ignores the NewRow.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With Dgw
    			.Columns.Add("One", "One")
    			.Columns.Add("Two", "Two")
    			.Columns.Add("DateDeCouvaison", "DateDeCouvaison")
    			.Columns.Add("Three", "Three")
    
    			For i As Integer = 0 To 9
    				.Rows.Add("AAA" & i.ToString, "BBB" & i.ToString, Now.AddDays(-i), "CCC" & i.ToString)
    			Next
    		End With
    	End Sub
    	Private Sub Dgw_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles Dgw.CellFormatting
    		If Not Dgw.Rows(e.RowIndex).IsNewRow Then
    			If e.ColumnIndex = Dgw.Columns("DateDeCouvaison").Index Then
    				e.CellStyle.Format = "d"
    				Dim TheDate As Date = CDate(e.Value)
    				Console.WriteLine(TheDate.ToShortDateString)
    				If TheDate.ToShortDateString = Now.AddDays(-2).ToShortDateString Then
    					e.CellStyle.BackColor = Color.SpringGreen
    					e.CellStyle.ForeColor = Color.Red
    				End If
    
    				If TheDate.ToShortDateString = Now.AddDays(-2).ToShortDateString Then
    					e.CellStyle.Font =
    							New Font(
    									"Segoe UI Semibold",
    									16.0!, FontStyle.Bold, GraphicsUnit.Point,
    									CType(0, Byte)
    							)
    				End If
    			End If
    		End If
    	End Sub
    End Class

    On the other hand, if you have the AllowUserToAddRows is set to False and the Totals are in the effective last row then this may be what you need (replace)

    'REPLACE If Not Dgw.Rows(e.RowIndex).IsNewRow Then
    If Not e.RowIndex = Dgw.Rows.Count - 1 Then
    


    Regards Les, Livingston, Scotland



    • Edited by leshay Sunday, June 16, 2019 12:47 PM
    Sunday, June 16, 2019 12:35 PM

  • I have tried your code and it seems to work OK here. It detects and changes the BackColor and Font of the correct cell(s) and ignores the NewRow.

    Regards Les, Livingston, Scotland


    Hi,

    Yes the code works Very Well, I want to exclude the last line of Cell Formating.

    Thank you very Much & Best regards.

    Sunday, June 16, 2019 12:51 PM
  • Yes the code works Very Well, I want to exclude the last line of Cell Formating.

    Thank you very Much & Best regards.

    Hi

    The replacement line of code I suggested may be what you want then.


    Regards Les, Livingston, Scotland

    • Marked as answer by Bajtitou Sunday, June 16, 2019 1:44 PM
    Sunday, June 16, 2019 1:30 PM
  • Yes the code works Very Well, I want to exclude the last line of Cell Formating.

    Thank you very Much & Best regards.

    Hi

    The replacement line of code I suggested may be what you want then.


    Regards Les, Livingston, Scotland

    Hi,

    Thank you Very Much,

    That's What I am looking for.

    Best regards

    Sunday, June 16, 2019 1:45 PM