dcsimg
Results 1 to 11 of 11

Thread: DataGridView Border Cell not appear during Form load at Row 0

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    DataGridView Border Cell not appear during Form load at Row 0

    Hi,

    I'm trying to make a border for the whole selected row in datagridview.
    However , when i load the Form , the border did appear on row 0. Once i push down key it will start appear at row 1.
    How to make it appear at row 0 when the Form first time load?
    Please help me to solve this problem

    Name:  NG at row 0.png
Views: 165
Size:  29.4 KB


    Her is my code

    Code:
    Imports System
    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    Imports System.Windows.Forms
    
    
    Public Class Form1
        Dim table As New DataTable("Table")
    
    
        Private Sub Form1_Load_1(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
    
            table.Columns.Add("Id", Type.GetType("System.String"))
            table.Columns.Add("DESC", Type.GetType("System.String"))
            table.Columns.Add("RELAY", Type.GetType("System.String"))
            table.Columns.Add("COMMAND", Type.GetType("System.String"))
            table.Columns.Add("DT", Type.GetType("System.String"))
          
    
            For j As Integer = 0 To 500
                table.Rows.Add("", "", "", "")
            Next
    
            DataGridView1.DataSource = table
    
    
            DataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
    
            DataGridView1.RowsDefaultCellStyle.BackColor = Color.FromArgb(16, 64, 16)  'utk jadikan warna DGV hijau gelap
    
    
        End Sub
    
    
        Private Sub DataGridView1_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    
            If e.RowIndex > -1 And e.ColumnIndex > -1 Then
    
                If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True Then
    
    
                    If (e.ColumnIndex) = 0 Then
    
    
                        e.Paint(e.CellBounds, DataGridViewPaintParts.All & DataGridViewPaintParts.Border)
    
                        Using cpen As New Pen(Color.Yellow, 1)
                            e.Graphics.DrawRectangle(cpen, e.CellBounds.X + 1, e.CellBounds.Y + 1, 497, e.CellBounds.Height - 3)
                        End Using
    
                        e.Handled = True
    
    
                    ElseIf (e.ColumnIndex) <> 0 Then
    
                        e.Paint(e.CellBounds, DataGridViewPaintParts.All & DataGridViewPaintParts.Border)
    
                        Using cpen As New Pen(Color.Yellow, 1)
                            e.Graphics.DrawRectangle(cpen, 0, e.CellBounds.Y + 1, 540, e.CellBounds.Height - 3)
                        End Using
    
                        e.Handled = True
    
                    End If
    
    
                ElseIf DataGridView1.SelectedCells.Count > 0 Then 'And DataGridView1.SelectedCells(0).RowIndex > 0 Then
    
                    If e.RowIndex = DataGridView1.SelectedCells(0).RowIndex Then
    
                        e.Paint(e.CellBounds, DataGridViewPaintParts.All & DataGridViewPaintParts.Border)
    
                        Using cpen As New Pen(Color.Yellow, 1)
                            e.Graphics.DrawRectangle(cpen, e.CellBounds.X + 1, e.CellBounds.Y + 1, 0, e.CellBounds.Height - 3)
                        End Using
    
                        e.Handled = True
    
                    Else
    
                    End If
    
                End If
            End If
    
    
    
        End Sub
    
    
    
    End Class

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,580

    Re: DataGridView Border Cell not appear during Form load at Row 0

    With every reservation as I'm very tired:
    If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True Then

    It says that if it is the selected row, start painting. If you need to appear on row 0 you need to change your logic.
    Again I may be wrong as I'm tired.
    Slow as hell.

  3. #3
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,471

    Re: DataGridView Border Cell not appear during Form load at Row 0

    Maybe add some code to select Row 0?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,270

    Re: DataGridView Border Cell not appear during Form load at Row 0

    The first thing to do is work out what "selected" means in this context and whether that is what you are actually talking about. In a DataGridView, a selected row is one that is in the SelectedRows collection of the parent DataGridView and has its Selected property set to True. In such cases, the row is generally highlighted, in blue by default. Note that the grid property is SelectedRows, not SelectedRow. That's because multiple rows can be selected at the same time, and the selection doesn't have to be contiguous.

    What often happens is that people talk about "the selected row" when what they actually mean is "the current row". The current row in a DataGridView is the row that contains the current cell and the current cell is the cell that currently has input focus. The grid has CurrentCell and CurrentRow properties for those. It also has a CurrentCellAddress property, which is likey what you should be using here. Compare e.RowIndex to CurrentCellAddress.Y to see whether you're painting a cell in the current row.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,270

    Re: DataGridView Border Cell not appear during Form load at Row 0

    I would also like to point out, as I often do, that this confusion is a direct result of not bothering to read the relevant documentation. Not surprisingly, you can learn the difference between selected rows and the current row from the documentation for the DataGridView class. That's exactly where I learned about it.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: DataGridView Border Cell not appear during Form load at Row 0

    HI,

    I've change mu code

    Code:
    Imports System
    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    Imports System.Windows.Forms
    
    
    Public Class Form1
        Dim table As New DataTable("Table")
    
    
        Private Sub Form1_Load_1(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
    
            table.Columns.Add("Id", Type.GetType("System.String"))
            table.Columns.Add("DESC", Type.GetType("System.String"))
            table.Columns.Add("RELAY", Type.GetType("System.String"))
            table.Columns.Add("COMMAND", Type.GetType("System.String"))
            table.Columns.Add("DT", Type.GetType("System.String"))
          
    
            For j As Integer = 0 To 500
                table.Rows.Add("", "", "", "")
            Next
    
            DataGridView1.DataSource = table
    
    
            DataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
    
            DataGridView1.RowsDefaultCellStyle.BackColor = Color.FromArgb(16, 64, 16)
           
    
            DataGridView1.CurrentCell = DataGridView1(0, 1)
            DataGridView1.CurrentCell = DataGridView1(0, 0)
    
        End Sub
    
    
        Private Sub DataGridView1_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    
            If e.RowIndex > -1 And e.ColumnIndex > -1 Then
    
                If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True Then
    
    
            
    
    
                    If (e.ColumnIndex) <> 0 Then
    
                             For i As Integer = 0 To DataGridView1.ColumnCount - 1
                            DataGridView1.Rows(e.RowIndex).Cells(i).Style.BackColor = Color.FromArgb(16, 64, 16)
                        Next
    
                        Using cpen As New Pen(Color.Yellow, 1)
                            e.Graphics.DrawRectangle(cpen, 0, e.CellBounds.Y + 1, 540, e.CellBounds.Height - 3)
                        End Using
    
                        e.Handled = True
    
                    End If
    
    
                End If
            End If
    
    
    
        End Sub
    
    
    
    End Class
    
    and it become like this when the form load.There is a white color fill in 2nd,3rd,4th and 5th column of row 0
    
    Name:  NG at row 0 column.png
    Views: 47
    Size:  42.8 KB

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,500

    Re: DataGridView Border Cell not appear during Form load at Row 0

    This seems to be the problem line...

    Code:
    If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True Then
    Try...

    Code:
    Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    
            If e.RowIndex > -1 And e.ColumnIndex > -1 Then
    
                If e.RowIndex = DataGridView1.CurrentRow.Index Then
    
    
    
    
    
                    If (e.ColumnIndex) <> 0 Then
    
                        For i As Integer = 0 To DataGridView1.ColumnCount - 1
                            DataGridView1.Rows(e.RowIndex).Cells(i).Style.BackColor = Color.FromArgb(16, 64, 16)
                        Next
    
                        Using cpen As New Pen(Color.Yellow, 1)
                            e.Graphics.DrawRectangle(cpen, 0, e.CellBounds.Y + 1, 540, e.CellBounds.Height - 3)
                        End Using
    
                        e.Handled = True
    
                    End If
    
    
                End If
            End If
    
    
    
        End Sub

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: DataGridView Border Cell not appear during Form load at Row 0

    It still give the same result, there are still white color fill in 2nd,3rd,4th and 5th column of row 0

  9. #9
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: DataGridView Border Cell not appear during Form load at Row 0

    Quote Originally Posted by art76 View Post
    It still give the same result, there are still white color fill in 2nd,3rd,4th and 5th column of row 0
    LOL.

    Its hard there are many possible settings for the dgv.

    This example starts up properly with the first row highlighted from the paint event drawing.

    The example makes the dgv so everything is setup to defaults and then addl settings in form load. To run just cut and paste the code into an empty form. Change the form name as reqd.

    Name:  a2.png
Views: 44
Size:  16.3 KB

    Code:
    Imports System.Drawing.Drawing2D
    
    Public Class Form4
        Private WithEvents dgv As New DataGridView With {.Parent = Me, .Dock = DockStyle.Fill}
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim columnA As New List(Of Single) From {0, 1, 1, 2, 0, 8, 8, 3}
            Dim columnB As New List(Of Single) From {4, 9, 9, 5, 9, 1, 9, 9}
            Dim columnC As New List(Of Single) From {0, 3, 7, 5, 1, 6, 0, 7}
    
            With dgv
                .SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
                .RowsDefaultCellStyle.BackColor = Color.FromArgb(16, 64, 16)
                .RowsDefaultCellStyle.ForeColor = Color.AntiqueWhite
                .RowHeadersVisible = False
    
                .Font = New Font("Arial", 10, FontStyle.Bold)
                .RowTemplate.Height = 20
                .ColumnCount = 3
                .Columns(0).Name = "Column A"
                .Columns(1).Name = "Column B"
                .Columns(2).Name = "Column C"
    
                'add the data to the grid
                For i As Integer = 0 To columnA.Count - 1
                    .Rows.Add(columnA(i).ToString, columnB(i).ToString, columnC(i).ToString)
                Next
    
            End With
        End Sub
    
        Private Sub dgv_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgv.CellPainting
    
            If e.RowIndex > -1 AndAlso e.RowIndex < dgv.Rows.Count AndAlso e.ColumnIndex > -1 Then
                If dgv.SelectedRows.Count > 0 Then
                    Using hBrush As New HatchBrush(HatchStyle.BackwardDiagonal, Color.DimGray, e.CellStyle.BackColor),
                            pY As New Pen(Color.Goldenrod, 1)
    
                        For Each row In dgv.SelectedRows
                            If row.Index = e.RowIndex Then
                                e.Graphics.FillRectangle(hBrush, e.CellBounds)
                                e.Graphics.DrawRectangle(pY, e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 2, e.CellBounds.Height - 3)
                                e.Graphics.DrawString(e.Value, e.CellStyle.Font, Brushes.Goldenrod, e.CellBounds.X + 2, e.CellBounds.Y + 1)
                                e.Handled = True
                            End If
                        Next
                    End Using
    
                End If
            End If
    
        End Sub
    
    End Class
    Last edited by tommytwotrain; Jul 11th, 2019 at 07:01 PM.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: DataGridView Border Cell not appear during Form load at Row 0

    Hi tommy,

    Your Idea is very nice, but if i want to change color from Brushes.Goldenrod to Color.FromArgb(96, 96, 255), how to do it ?
    Code:
    e.Graphics.DrawString(e.Value, e.CellStyle.Font, Brushes.Goldenrod, e.CellBounds.X + 2, e.CellBounds.Y + 1)

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: DataGridView Border Cell not appear during Form load at Row 0

    Thank you all. I already found solution to change color from Brushes.Goldenrod to Color.FromArgb(96, 96, 255)

    Code:
    e.Graphics.DrawString(e.Value, New Font("Utsaah", 18, FontStyle.Bold), New SolidBrush(Color.FromArgb(96, 96, 255)), e.CellBounds.X + 2, e.CellBounds.Y + 1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width