Question : Custom DataGridView cell definition

I have built an extended version of the DataGridView control which is used in an unbound mode and I want to have a custom cell definition for a portion of the matrix. Each custom cell can hold only two characters and each character can have a different backgound color. For example, a cell could have a green space (which would look like a green block, and an "X" with a red background. The colors and characters can change independently. I presume I create a class that inherits "cell" but beyond that it gets a bit fuzzy.

Answer : Custom DataGridView cell definition

THis was a nightmare in .net1.x but the datgridview makes it sooo easy...
Just handle the paint event for the cell and draw on what you want...

    Private Sub grd_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles grdScheduler.CellPainting, grdModifications.CellPainting
        If e.ColumnIndex = 3 And e.RowIndex >= 0 Then 'Paint The Words on the Status Field
            Dim displayvalue As String = ""
            Select Case UCase(CheckNullString(e.Value)).Substring(0, 1)
                Case "H"
                    displayvalue = "Hold"
                Case "N"
                    displayvalue = "Not Yet Started"
                Case "S"
                    displayvalue = "Succesful"
                Case "Y"
                    displayvalue = "Running"
                Case "F"
                    displayvalue = "Failed"
                Case Else
                    displayvalue = e.Value
            End Select

            DataGridViewDrawCellText(CType(sender, DataGridView), e, displayvalue)
            e.Handled = True
        End If


    End Sub


Public Shared Sub DataGridViewDrawCellText(ByRef objCurrentDataGridView As DataGridView, ByRef objDataGridViewCellPaintingEventArgs As DataGridViewCellPaintingEventArgs, ByVal TextToDraw As String)

        Dim newRect As New Rectangle(objDataGridViewCellPaintingEventArgs.CellBounds.X + 1, objDataGridViewCellPaintingEventArgs.CellBounds.Y + 1, _
                               objDataGridViewCellPaintingEventArgs.CellBounds.Width - 4, objDataGridViewCellPaintingEventArgs.CellBounds.Height - 4)
        Dim backColorBrush As New SolidBrush(objDataGridViewCellPaintingEventArgs.CellStyle.BackColor)
        Dim gridBrush As New SolidBrush(objCurrentDataGridView.GridColor)
        Dim gridLinePen As New Pen(gridBrush)

        Try

            ' Erase the cell.
            objDataGridViewCellPaintingEventArgs.Graphics.FillRectangle(backColorBrush, objDataGridViewCellPaintingEventArgs.CellBounds)

            ' Draw the grid lines (only the right and bottom lines;
            ' DataGridView takes care of the others).
            objDataGridViewCellPaintingEventArgs.Graphics.DrawLine(gridLinePen, objDataGridViewCellPaintingEventArgs.CellBounds.Left, _
                objDataGridViewCellPaintingEventArgs.CellBounds.Bottom - 1, objDataGridViewCellPaintingEventArgs.CellBounds.Right - 1, _
                objDataGridViewCellPaintingEventArgs.CellBounds.Bottom - 1)
            objDataGridViewCellPaintingEventArgs.Graphics.DrawLine(gridLinePen, objDataGridViewCellPaintingEventArgs.CellBounds.Right - 1, _
                objDataGridViewCellPaintingEventArgs.CellBounds.Top, objDataGridViewCellPaintingEventArgs.CellBounds.Right - 1, _
                objDataGridViewCellPaintingEventArgs.CellBounds.Bottom)

            ' Draw the inset highlight box.
            '  objDataGridViewCellPaintingEventArgs.Graphics.DrawRectangle(Pens.Blue, newRect)

            ' Draw the text content of the cell, ignoring alignment.
            If Not (objDataGridViewCellPaintingEventArgs.Value Is Nothing) Then

                objDataGridViewCellPaintingEventArgs.Graphics.DrawString(TextToDraw, objDataGridViewCellPaintingEventArgs.CellStyle.Font, _
                Brushes.Black, objDataGridViewCellPaintingEventArgs.CellBounds.X + 2, objDataGridViewCellPaintingEventArgs.CellBounds.Y + 2, _
                StringFormat.GenericDefault)
            End If
            objDataGridViewCellPaintingEventArgs.Handled = True

        Finally
            gridLinePen.Dispose()
            gridBrush.Dispose()
            backColorBrush.Dispose()
        End Try
    End Sub


In this example i just paint the word 'Hold' where the cell contains 'H' but you could easily change this for your purposes...


hth..
Random Solutions  
 
programming4us programming4us