Question DataGridView padding still there even though it's set to zero

Runescope

Well-known member
Joined
Jan 6, 2011
Messages
53
Programming Experience
Beginner
Greepings,

I've been banging my head on this for a few hours now and I can't seem to figure out a way to get around it. I've got a datagridview setup with lots of empty columns and rows. The user clicks into a cell to fill it with a pattern they've picked. I want the patter to go right to the edge of the cell, but I can't seem to get rid of the padding, even though I've set everything to zero and even when I do a test and ask it to report the padding for that cell, it says it's zero, but it most certainly is not.

Increasing the font size just pushes it off the right side of the cell while leaving a blank space on the left. I've changed alignments to top-left, top right, center, etc etc with no luck.
Anyone have an idea?

Image as an example.

Conversely, if someone has a better way of doing this without a datagridview, I would be MORE than happy to hear about it.

Padding.png
 
Last edited:
How you're actually getting the pattern into the cells is rather relevant information. Are we talking Images in Image cells? GDI+ on the CellPainting event of TextBox cells? Something else? You should provide enough information that we can replicate the problem in order to test possible solutions. In Stack Overflow terms, a minimal, verifiable, complete example.
 
Ugh right, sorry. Didn't even think of that. It's straight text from a font I have of different shapes.

They pick the pattern they want, it goes into the text of a label then they click on a cell in the datagridview which places the text into the cell. It's a simple app, I've tried to keep it as simple as possible.

Here's the entire code:

VB.NET:
Public Class frmMain
    Public objSenderName As System.Object
    Public transparentImage As Bitmap
    Public Class NoEllipsisColumn
        Inherits DataGridViewTextBoxColumn
        Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
            Get
                Return New NoEllipsisCell
            End Get
            Set(ByVal value As System.Windows.Forms.DataGridViewCell)
                MyBase.CellTemplate = value
            End Set
        End Property
    End Class

    Public Class NoEllipsisCell
        Inherits DataGridViewTextBoxCell
        Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
            paintParts = paintParts And Not DataGridViewPaintParts.ContentForeground
            MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
            Dim format As New StringFormat(StringFormatFlags.NoWrap)
            format.LineAlignment = StringAlignment.Center
            format.Trimming = StringTrimming.Word 'or Character 
            Dim brush As New SolidBrush(cellStyle.ForeColor)
            graphics.DrawString(formattedValue, cellStyle.Font, brush, cellBounds, format)
            brush.Dispose()
            format.Dispose()
        End Sub
    End Class

    Private Sub lblKnot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblKnotAA.Click, lblKnotAB.Click, lblKnotAC.Click, lblKnotAD.Click, lblKnotAE.Click, lblKnotAF.Click, lblKnotAG.Click, lblKnotAH.Click, lblKnotAI.Click, lblKnotAJ.Click, lblKnotAK.Click, lblKnotAL.Click, lblKnotAM.Click,
        lblKnotBA.Click, lblKnotBB.Click, lblKnotBC.Click, lblKnotBD.Click, lblKnotBE.Click, lblKnotBF.Click, lblKnotBG.Click, lblKnotBH.Click, lblKnotBI.Click, lblKnotBJ.Click, lblKnotBK.Click, lblKnotBL.Click, lblKnotBM.Click, lblKnotCA.Click, lblKnotCB.Click, lblKnotCC.Click, lblKnotCD.Click, lblKnotCE.Click, lblKnotCF.Click, lblKnotCG.Click, lblKnotCH.Click, lblKnotCI.Click, lblKnotCJ.Click, lblKnotCK.Click, lblKnotCL.Click, lblKnotCM.Click,
        lblKnotDA.Click, lblKnotDB.Click, lblKnotDC.Click, lblKnotDD.Click, lblKnotDE.Click, lblKnotDF.Click, lblKnotDG.Click, lblKnotDH.Click, lblKnotDI.Click, lblKnotDJ.Click, lblKnotDK.Click, lblKnotDL.Click, lblKnotDM.Click, lblKnotEA.Click, lblKnotEB.Click, lblKnotEC.Click, lblKnotED.Click, lblKnotEE.Click, lblKnotEF.Click, lblKnotEG.Click, lblKnotEH.Click, lblKnotEI.Click, lblKnotEJ.Click, lblKnotEK.Click, lblKnotEL.Click, lblKnotEM.Click,
        lblKnotFA.Click, lblKnotFB.Click, lblKnotFC.Click, lblKnotFD.Click, lblKnotFE.Click, lblKnotFF.Click, lblKnotFG.Click, lblKnotFH.Click, lblKnotFI.Click, lblKnotFJ.Click, lblKnotFK.Click, lblKnotFL.Click, lblKnotFM.Click, lblKnotGA.Click, lblKnotGB.Click, lblKnotGC.Click, lblKnotGD.Click, lblKnotGE.Click, lblKnotGF.Click, lblKnotGG.Click, lblKnotGH.Click, lblKnotGI.Click, lblKnotGJ.Click, lblKnotGK.Click, lblKnotGL.Click, lblKnotGM.Click,
        lblKnotHA.Click, lblKnotHB.Click, lblKnotHC.Click, lblKnotHD.Click, lblKnotHE.Click, lblKnotHF.Click, lblKnotHG.Click, lblKnotHH.Click, lblKnotHI.Click, lblKnotHJ.Click, lblKnotHK.Click, lblKnotHL.Click, lblKnotHM.Click, lblKnotIA.Click, lblKnotIB.Click, lblKnotIC.Click, lblKnotID.Click, lblKnotIE.Click, lblKnotIF.Click, lblKnotIG.Click, lblKnotIH.Click, lblKnotII.Click, lblKnotIJ.Click, lblKnotIK.Click, lblKnotIL.Click, lblKnotIM.Click,
        lblKnotJA.Click, lblKnotJB.Click, lblKnotJC.Click, lblKnotJD.Click, lblKnotJE.Click, lblKnotJF.Click, lblKnotJG.Click, lblKnotJH.Click, lblKnotJI.Click, lblKnotJJ.Click, lblKnotJK.Click, lblKnotJL.Click, lblKnotJM.Click, lblKnotKA.Click, lblKnotKC.Click, lblKnotKD.Click, lblKnotKE.Click, lblKnotKF.Click, lblKnotKG.Click, lblKnotKH.Click, lblKnotKI.Click, lblKnotKJ.Click, lblKnotKK.Click, lblKnotKM.Click, lblKnotLA.Click, lblKnotLB.Click,
        lblKnotLC.Click, lblKnotLD.Click, lblKnotLE.Click, lblKnotLF.Click, lblKnotLG.Click, lblKnotLH.Click, lblKnotLI.Click, lblKnotLJ.Click, lblKnotLK.Click, lblKnotLL.Click, lblKnotLM.Click, lblKnotMB.Click, lblKnotMC.Click, lblKnotMD.Click, lblKnotME.Click, lblKnotMF.Click, lblKnotMG.Click, lblKnotMH.Click, lblKnotMI.Click, lblKnotMJ.Click, lblKnotMK.Click, lblKnotML.Click, lblKnotNA.Click, lblKnotNC.Click, lblKnotND.Click, lblKnotNE.Click,
        lblKnotNF.Click, lblKnotNG.Click, lblKnotNH.Click, lblKnotNI.Click, lblKnotNJ.Click, lblKnotNK.Click, lblKnotNM.Click, lblKnotOA.Click, lblKnotOB.Click, lblKnotOC.Click, lblKnotOD.Click, lblKnotOE.Click, lblKnotOF.Click, lblKnotOG.Click, lblKnotOH.Click, lblKnotOI.Click, lblKnotOJ.Click, lblKnotOK.Click, lblKnotOL.Click, lblKnotOM.Click, lblKnotPB.Click, lblKnotPC.Click, lblKnotPD.Click, lblKnotPE.Click, lblKnotPF.Click, lblKnotPG.Click,
        lblKnotPH.Click, lblKnotPI.Click, lblKnotPJ.Click, lblKnotPK.Click, lblKnotPL.Click, lblKnotPM.Click, lblKnotQM.Click


        If IsNothing(objSenderName) = False Then
            objSenderName.BorderStyle = BorderStyle.None
        End If
        lblKnotDisp.Text = sender.Text
        sender.BorderStyle = BorderStyle.FixedSingle
        objSenderName = sender
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        transparentImage = New Bitmap(30, 30)
        Dim graphics As Graphics = graphics.FromImage(transparentImage)
        graphics.FillRectangle(Brushes.Transparent, 0, 0, 30, 30)
        graphics.Dispose()

        numRows.Value = 30
        numColumns.Value = 45
        FillGrid()
    End Sub

    Public Sub FillGrid()

        dgvPattern.Columns.Clear()

        Dim RTotal As Integer = numRows.Value : If RTotal = 0 Then RTotal = 30
        Dim CTotal As Integer = numColumns.Value : If CTotal = 0 Then CTotal = 45

        For cIdx = 1 To CTotal
            'dgvPattern.Columns.Add(New DataGridViewTextBoxColumn)
            dgvPattern.Columns.Add(New NoEllipsisColumn)
            'dgvPattern.Columns.Add(New DataGridViewImageColumn)
            dgvPattern.Columns(cIdx - 1).Width = 20
        Next

        dgvPattern.DefaultCellStyle.WrapMode = DataGridViewTriState.False
        dgvPattern.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight
        dgvPattern.Rows.Add(RTotal)
        dgvPattern.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight
    End Sub

    Private Sub numRows_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numRows.ValueChanged
        FillGrid()

    End Sub

    Private Sub numColumns_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numColumns.ValueChanged
        FillGrid()
    End Sub

    Private Sub dgvPattern_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvPattern.CellMouseClick
        'dgvPattern(e.ColumnIndex, e.RowIndex).DataGridView.DefaultCellStyle.Padding = New Padding(0, 0, 0, 0)
        dgvPattern(e.ColumnIndex, e.RowIndex).Value = lblKnotDisp.Text
        Debug.Print(dgvPattern(e.ColumnIndex, e.RowIndex).DataGridView.DefaultCellStyle.Padding.Top.ToString)
        Debug.Print(dgvPattern(e.ColumnIndex, e.RowIndex).DataGridView.DefaultCellStyle.Padding.Bottom.ToString)
        Debug.Print(dgvPattern(e.ColumnIndex, e.RowIndex).DataGridView.DefaultCellStyle.Padding.Left.ToString)
        Debug.Print(dgvPattern(e.ColumnIndex, e.RowIndex).DataGridView.DefaultCellStyle.Padding.Right.ToString)
    End Sub
End Class
 
You know, if anyone has a better (or even comparable) idea on how to layout a grid that can be easily adjusted by the user and then filled with an image or text, I am more than happy to hear it. I'm getting nowhere with this.
 
Back
Top