Changing datagridview cells borders at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • steve

    #1

    Changing datagridview cells borders at runtime

    Hi All

    I urgently need help on setting datagridview cell borders at runtime

    I found some code on the web from
    Programming Smart Client Data Applications with .NET 2.0
    by Brian Noyes
    See below

    This is what I have been trying to achieve, but when I run it ALL the cell
    Top borders go from inset to feint white on ALL cells, ALL rows

    If I change the line (marked *******) to false then the borders are OK but
    obviously no hiding of the required cell top borders

    I am completely lost on what is wrong

    Any help greatly appreciated

    Regards
    Steve

    Public Class GroupByGrid

    Inherits DataGridView

    Public Sub New()

    With Me

    ..AdvancedCellB orderStyle.All = DataGridViewAdv ancedCellBorder Style.Inset

    End With

    End Sub

    Protected Overrides Sub OnCellFormattin g(ByVal args As
    DataGridViewCel lFormattingEven tArgs)

    ' Call home to base

    MyBase.OnCellFo rmatting(args)

    ' First row always displays

    If args.RowIndex = 0 Then

    Return

    End If

    If IsRepeatedCellV alue(args.RowIn dex, args.ColumnInde x) Then

    args.Value = String.Empty

    args.Formatting Applied = True

    End If

    End Sub

    Private Function IsRepeatedCellV alue(ByVal rowIndex As Integer, ByVal
    colIndex As Integer) As Boolean

    Dim currCell As DataGridViewCel l = Rows(rowIndex). Cells(colIndex)

    Dim prevCell As DataGridViewCel l = Rows(rowIndex - 1).Cells(colInd ex)

    If Not IsNothing(currC ell) AndAlso Not IsNothing(prevC ell) AndAlso Not
    IsNothing(currC ell.Value) AndAlso Not IsNothing(prevC ell.Value) Then

    If (currCell.Value .ToString() = prevCell.Value. ToString() OrElse
    currCell.Value. Equals(prevCell .Value)) And prevCell.Value. ToString <>
    String.Empty And currCell.Value. ToString <> String.Empty Then

    Return true *************** *************** *************** *

    Else

    Return False

    End If

    Else

    Return False

    End If

    End Function

    Protected Overrides Sub OnCellPainting( ByVal args As
    DataGridViewCel lPaintingEventA rgs)

    MyBase.OnCellPa inting(args)

    ' Ignore column and row headers and first row

    If args.RowIndex < 1 OrElse args.ColumnInde x < 0 Then

    Return

    End If

    If IsRepeatedCellV alue(args.RowIn dex, args.ColumnInde x) Then

    args.AdvancedBo rderStyle.Top = DataGridViewAdv ancedCellBorder Style.None

    Else

    args.AdvancedBo rderStyle.Top = AdvancedCellBor derStyle.Top

    End If



    End Sub

    End Class


  • Cor Ligthert [MVP]

    #2
    Re: Changing datagridview cells borders at runtime

    steve,

    I gave you the solution that was used in past with a datagrid, did you try
    that?

    Cor

    "steve" <ga630sf@newsgr oups.nospam> schreef in bericht
    news:eNrDUvpkGH A.1204@TK2MSFTN GP02.phx.gbl...[color=blue]
    > Hi All
    >
    > I urgently need help on setting datagridview cell borders at runtime
    >
    > I found some code on the web from
    > Programming Smart Client Data Applications with .NET 2.0
    > by Brian Noyes
    > See below
    >
    > This is what I have been trying to achieve, but when I run it ALL the cell
    > Top borders go from inset to feint white on ALL cells, ALL rows
    >
    > If I change the line (marked *******) to false then the borders are OK but
    > obviously no hiding of the required cell top borders
    >
    > I am completely lost on what is wrong
    >
    > Any help greatly appreciated
    >
    > Regards
    > Steve
    >
    > Public Class GroupByGrid
    >
    > Inherits DataGridView
    >
    > Public Sub New()
    >
    > With Me
    >
    > .AdvancedCellBo rderStyle.All = DataGridViewAdv ancedCellBorder Style.Inset
    >
    > End With
    >
    > End Sub
    >
    > Protected Overrides Sub OnCellFormattin g(ByVal args As
    > DataGridViewCel lFormattingEven tArgs)
    >
    > ' Call home to base
    >
    > MyBase.OnCellFo rmatting(args)
    >
    > ' First row always displays
    >
    > If args.RowIndex = 0 Then
    >
    > Return
    >
    > End If
    >
    > If IsRepeatedCellV alue(args.RowIn dex, args.ColumnInde x) Then
    >
    > args.Value = String.Empty
    >
    > args.Formatting Applied = True
    >
    > End If
    >
    > End Sub
    >
    > Private Function IsRepeatedCellV alue(ByVal rowIndex As Integer, ByVal
    > colIndex As Integer) As Boolean
    >
    > Dim currCell As DataGridViewCel l = Rows(rowIndex). Cells(colIndex)
    >
    > Dim prevCell As DataGridViewCel l = Rows(rowIndex - 1).Cells(colInd ex)
    >
    > If Not IsNothing(currC ell) AndAlso Not IsNothing(prevC ell) AndAlso Not
    > IsNothing(currC ell.Value) AndAlso Not IsNothing(prevC ell.Value) Then
    >
    > If (currCell.Value .ToString() = prevCell.Value. ToString() OrElse
    > currCell.Value. Equals(prevCell .Value)) And prevCell.Value. ToString <>
    > String.Empty And currCell.Value. ToString <> String.Empty Then
    >
    > Return true *************** *************** *************** *
    >
    > Else
    >
    > Return False
    >
    > End If
    >
    > Else
    >
    > Return False
    >
    > End If
    >
    > End Function
    >
    > Protected Overrides Sub OnCellPainting( ByVal args As
    > DataGridViewCel lPaintingEventA rgs)
    >
    > MyBase.OnCellPa inting(args)
    >
    > ' Ignore column and row headers and first row
    >
    > If args.RowIndex < 1 OrElse args.ColumnInde x < 0 Then
    >
    > Return
    >
    > End If
    >
    > If IsRepeatedCellV alue(args.RowIn dex, args.ColumnInde x) Then
    >
    > args.AdvancedBo rderStyle.Top = DataGridViewAdv ancedCellBorder Style.None
    >
    > Else
    >
    > args.AdvancedBo rderStyle.Top = AdvancedCellBor derStyle.Top
    >
    > End If
    >
    >
    >
    > End Sub
    >
    > End Class
    >
    >[/color]


    Comment

    • Linda Liu [MSFT]

      #3
      RE: Changing datagridview cells borders at runtime

      Hi Steve,

      Thank you for posting.

      I perfomed a test based on your code and reproduce the problem you have
      described. Through debugging, I found out the following facts:

      1. Once you changed the args.AdvancedBo rderStyle.Top to
      DataGridViewAdv ancedCellBorder Style.None in the OnCellPainting method, the
      AdvancedBorderS tyle.Top of the DataGridView is changed to
      DataGridViewAdv ancedCellBorder Style.None too.

      2. Once the AdvancedBorderS tyle.Top of the DataGridView has been changed to
      a different value from previous, the CellPainting event of the DataGridView
      will be raised automatically. Thus the OnCellPainting method is called. All
      cells in the DataGridView will be in the same border style at last.

      These two facts explain why the problem occurs.

      I have tried modifying the statement
      "args.AdvancedB orderStyle.Top= AdvancedCellBor derStyle.Top" to
      the "args.AdvancedB orderStyle.Top =
      DataGridViewAdv ancedCellBorder Style.Inset". When running, the result turned
      out to be correct--only those cells having the same value hide their top
      borders. However, the DataGridView seems to be dithering. The
      OnCellPainting method is being called again an again and this calling seems
      to never stop. So this modification is not good.

      It seems that we have to do this on other way. I will go on researching
      this issue. I appreciate your patience.



      Sincerely,
      Linda Liu
      Microsoft Online Community Support

      =============== =============== =============== =======
      When responding to posts,please "Reply to Group" via
      your newsreader so that others may learn and benefit
      from your issue.
      =============== =============== =============== =======

      Comment

      • Jeffrey Tan[MSFT]

        #4
        RE: Changing datagridview cells borders at runtime

        Hi steve,

        Have you read my reply to you in your another post? You'd better override
        AdjustCellBorde rStyle method to achieve this task. If you still have any
        concern, please feel free to followup me in that post. Thanks!

        Best regards,
        Jeffrey Tan
        Microsoft Online Community Support
        =============== =============== =============== =====
        When responding to posts, please "Reply to Group" via your newsreader so
        that others may learn and benefit from your issue.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        • steve

          #5
          Re: Changing datagridview cells borders at runtime

          Hi Jeff

          Thanks for all your efforts on this topic

          I hadn't seen your final reply to my old post on this topic but have now
          printed it and looking into it

          I have been able to get what I want using an override onpaint event but it
          only works when the border style is set to single
          (see prev reply on this thread from Linda Liu)
          and I would prefer to use raised or sunken

          Kind regards anyway
          Steve


          ""Jeffrey Tan[MSFT]"" <jetan@online.m icrosoft.com> wrote in message
          news:yu1JSwPlGH A.4928@TK2MSFTN GXA01.phx.gbl.. .[color=blue]
          > Hi steve,
          >
          > Have you read my reply to you in your another post? You'd better override
          > AdjustCellBorde rStyle method to achieve this task. If you still have any
          > concern, please feel free to followup me in that post. Thanks!
          >
          > Best regards,
          > Jeffrey Tan
          > Microsoft Online Community Support
          > =============== =============== =============== =====
          > When responding to posts, please "Reply to Group" via your newsreader so
          > that others may learn and benefit from your issue.
          > =============== =============== =============== =====
          > This posting is provided "AS IS" with no warranties, and confers no
          > rights.
          >[/color]


          Comment

          • steve

            #6
            Re: Changing datagridview cells borders at runtime

            Hi Jeff

            I hadn't looked at this group for a while assuming the thread was at an end

            I have tried the code you attached but I get an error message at the
            ValidateRect(Me .DataGridView1. Handle, Nothing) line

            A call to PInvoke function
            'DataGridViewPa inting!DataGrid ViewPainting.Fo rm1::ValidateRe ct' has
            unbalanced the stack. This is likely because the managed PInvoke signature
            does not match the unmanaged target signature. Check that the calling
            convention and parameters of the PInvoke signature match the target
            unmanaged signature.



            Regards

            Steve



            ""Jeffrey Tan[MSFT]"" <jetan@online.m icrosoft.com> wrote in message
            news:ij7huoQmGH A.4928@TK2MSFTN GXA01.phx.gbl.. .[color=blue]
            > Hi Steve,
            >
            > Have you recieved my sample project? Does it make sense to you? If you
            > still need any help, please feel free to tell me.
            >
            > Additionally, I found that there is some strange performance issue in my
            > sample project. After performing some debugging I found that changing
            > DataGridView.Ad vancedCellBorde rStyle property in CellPainting event will
            > generate another WM_PAINT message and send to DataGridView, which will
            > force another painting in DataGridView(ag ain calls CellPainting event ),
            > so
            > the DataGridView UI will keep painting all the time and have a flicker UI.
            >
            > To resolve this issue, you may place a ValidateRect Win32 calling in
            > CellPainting event, which removes the WM_PAINT message in the message
            > queue. Attached is the modified sample project, which works well now. Hope
            > it helps!
            >
            > Best regards,
            > Jeffrey Tan
            > Microsoft Online Community Support
            > =============== =============== =============== =====
            > When responding to posts, please "Reply to Group" via your newsreader so
            > that others may learn and benefit from your issue.
            > =============== =============== =============== =====
            > This posting is provided "AS IS" with no warranties, and confers no
            > rights.[/color]


            Comment

            • steve

              #7
              Re: Changing datagridview cells borders at runtime

              Hi Jeffrey

              Thanks for the help on this topic

              I have got it working just fine

              Regards
              Steve
              ""Jeffrey Tan[MSFT]"" <jetan@online.m icrosoft.comwro te in message
              news:J9y4CG2mGH A.5268@TK2MSFTN GXA01.phx.gbl.. .
              Hi Steve,
              >
              Thanks for your feedback!
              >
              Oh, yes, I original tested the project in release build which can not see
              this error. By testing it under debugger, I can see this error. The error
              is caused by an improper API declaration: the second parameter to
              ValidateRect should be ByRef instead of ByVal. However, after correcting
              this error, it seems that it does not take effect. So it seems that
              ValidateRect can not remove WM_PAINT message from the message queue.
              >
              Currently, I think the only valid solution is overriding
              AdjustCellBorde rStyle method in DataGridViewCel l and return a customized
              cell. I have written a sample project to demonstrate this technology,
              please see the attachment. Just click the button on the Form to see the
              effect.
              >
              Hope it helps!
              >
              Best regards,
              Jeffrey Tan
              Microsoft Online Community Support
              =============== =============== =============== =====
              When responding to posts, please "Reply to Group" via your newsreader so
              that others may learn and benefit from your issue.
              =============== =============== =============== =====
              This posting is provided "AS IS" with no warranties, and confers no
              rights.

              Comment

              • Jeffrey Tan[MSFT]

                #8
                Re: Changing datagridview cells borders at runtime

                You are welcome.

                Best regards,
                Jeffrey Tan
                Microsoft Online Community Support
                =============== =============== =============== =====
                When responding to posts, please "Reply to Group" via your newsreader so
                that others may learn and benefit from your issue.
                =============== =============== =============== =====
                This posting is provided "AS IS" with no warranties, and confers no rights.

                Comment

                Working...