Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Datagridview Cellformatting

Datagridview Cellformatting

Scheduled Pinned Locked Moved Visual Basic
helpcsharpdatabasevisual-studio
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chrispie123
    wrote on last edited by
    #1

    Hi I need help regarding the cell formatting event for the datagridview control in windows forms. I looked on the web and I cannot seem to find a perfect solution for this. I am using VS 2008 and code in VB.NET. I have a datagridview with many fields amongst which I have an Effective from date. If the effective from date is smaller than today’s date certain fields should be disabled and made a funny color.

    Private Sub DGV1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGV1.CellFormatting
    Try
    If e.RowIndex >= -1 And e.RowIndex <= DGV1.RowCount Then 'Basic index validation
    If Not ((DGV1 Is Nothing) OrElse (DGV1.Rows(e.RowIndex) Is Nothing) OrElse (DGV1.Rows(e.RowIndex).DataBoundItem Is Nothing)) Then

    		Dim dgvRowState As DataRowState = DirectCast(DGV1.Rows(e.RowIndex).DataBoundItem, DataRowView).Row.RowState
    		
    		'The idea here is to check that it is only for Non Added/Detatched rows where the Effective From Date is smaller than today
    		If Not (dgvRowState = DataRowState.Added OrElse dgvRowState = DataRowState.Detached) \_
    		   AndAlso e.ColumnIndex = EffectFromDataGridViewTextBoxColumn.Index AndAlso CDate(e.Value).Date < Date.Now.Date Then
    			e.CellStyle.BackColor = My.Settings.ReadOnlyColor
    			DGV1.Rows(e.RowIndex).Cells(e.ColumnIndex).ReadOnly = True
    			DGV1.Rows(e.RowIndex).Cells(Field1.Index).ReadOnly = True
    			DGV1.Rows(e.RowIndex).Cells(Field2.Index).ReadOnly = True				
    
    			DGV1.Rows(e.RowIndex).Cells(Field1.Index).Style.BackColor = My.Settings.ReadOnlyColor                            
    			DGV1.Rows(e.RowIndex).Cells(Field2.Index).Style.BackColor = My.Settings.ReadOnlyColor
    			DGV1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = My.Settings.ReadOnlyColor
    			
    		End If
    	End If
    End If
    

    Catch ex As IndexOutOfRangeException
    Exit Sub 'Exception added to aviod the following behaviour... When the user adds a new line enters a value and use the mouse to
    ' navigate to another line, the current new row will be cleared and the cellEndEdit event will be fired. inwhich case the DGV contains
    ' a blank row that is not dirty. The row "DGV1.Rows(RowIndex).DataBoundItem" part gives an exception for this row
    End Try

    This works well… But the problem I am having is when I go to a row where the Effective from date is greater than today and where cells is not read only and change the effective from date to the past that row also changes. I don’t want this to h

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups