Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Set DataGridViewCell borders to specific cells

$
0
0

I have a DataGridView with 3 columns. The GridView has FixedSingle Border and CellStyles are default. So when I run the program, I see a Grid with Black border and each row/cell with a faint off-white border. I want to remove the left border of 1st cell and right border of last cell in each row. Background colour of DataGridView is white. Here is my code. But it does not do anything.  If I set the cell border to White, all the cell borders become white and the grid looks like numbers arranged in a matrix. Need help...

 

Private Sub FillGrid()

dgvCutouts.AutoGenerateColumns = False
        dgvCutouts.Columns.Clear()
        dgvCutouts.DataSource = dtCutout.DefaultView

        Dim lblSize As New DataGridViewLabelXColumn
        lblSize.Name = "lblSize"
        lblSize.HeaderText = "Size"
        lblSize.DataPropertyName = "Size"
        lblSize.Width = 120
        lblSize.BorderSide = eBorderSide.Bottom And eBorderSide.Right And eBorderSide.Top
        dgvCutouts.Columns.Add(lblSize)

                Dim txtNumber As New DataGridViewTextBoxColumn
        txtNumber.Name = "txtNumber"
        txtNumber.HeaderText = "Numbers"
        txtNumber.Width = 60
        txtNumber.DataPropertyName = "Number"
        dgvCutouts.Columns.Add(txtNumber)

              Dim lblElementIDs As New DataGridViewLabelXColumn
        lblElementIDs.Name = "lbIDs"
        lblElementIDs.Visible = False
        lblElementIDs.DataPropertyName = "IDs"
        dgvCutouts.Columns.Add(lblIDs)

        AddHandler dgvCutouts.CellPainting, AddressOf dgvCutouts_CellPainting
        AddHandler dgvCutouts.CurrentCellChanged, AddressOf dgvCutouts_CurrentCellChanged

        For Each cell As DataGridViewCell In dgvCutouts.SelectedCells
            If cell.Selected = True Then cell.Selected = False
        Next

End Sub

 Private Sub dgvCutouts_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)

        If e.RowIndex >= 0 AndAlso (e.ColumnIndex = 0 Or e.ColumnIndex = dgvCutouts.ColumnCount - 1) Then

            Dim p1 As Point = New Point(e.CellBounds.X, e.CellBounds.Y - 1)
            Dim p2 As Point = New Point(e.CellBounds.X, e.CellBounds.Y + e.CellBounds.Height)
            e.Graphics.DrawLine(New Pen(Color.Blue), p1, p2)

            Dim p3 As Point = New Point(e.CellBounds.X + e.CellBounds.Width, e.CellBounds.Y)
            Dim p4 As Point = New Point(e.CellBounds.X + e.CellBounds.Width, e.CellBounds.Y + e.CellBounds.Height)
            e.Graphics.DrawLine(New Pen(Color.Blue), p3, p4)

            Dim p5 As Point = New Point(e.CellBounds.X - 1, e.CellBounds.Y)
            Dim p6 As Point = New Point(e.CellBounds.X - 1, e.CellBounds.Y + e.CellBounds.Height)

            End If


    End Sub

    Private Sub dgvCutouts_CurrentCellChanged(ByVal sender As Object, ByVal e As EventArgs)
        dgvCutouts.Refresh()
    End Sub


Viewing all articles
Browse latest Browse all 2535

Trending Articles