Results 1 to 15 of 15

Thread: Inherited DataGridView Column Double Up

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Location
    Perth, Australia
    Posts
    101

    Inherited DataGridView Column Double Up

    I am creating a little timesheet app, and I have created my own custom datagridview. I have got it all setup and it looks fine when I first add the control to the form.

    When I debug the app for the first time after adding, I get a double up of columns. Does anyone know what causes this?
    Attached Images Attached Images   

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Inherited DataGridView Column Double Up

    Are the columns added in code? Have you actually checked whether the code is executed twice?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Inherited DataGridView Column Double Up

    I'm having this same problem.
    In my app, Columns are manually added in the New() sub.
    In design view, it has the right number of columns, but when I run the app, it doubles up the columns.
    Does vb run the New() sub twice?
    I even added Columns.Clear in the New() sub, in case the New() sub was being run twice, but that didn't fix it.
    The form designer code shows only the 1st set of columns.

  4. #4
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Inherited DataGridView Column Double Up

    Perhaps this is part of the problem?
    The original URL is missing and I don't understand what needs to be done to fix this problem.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Inherited DataGridView Column Double Up

    Nope. If there was a problem with inheriting DataGridView then it's been fixed. I'm not getting any problems with it so it's gotta be in your code somewhere. I'd suggest you show us at least you New Sub!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Inherited DataGridView Column Double Up

    Quote Originally Posted by dunfiddlin View Post
    Nope. If there was a problem with inheriting DataGridView then it's been fixed. I'm not getting any problems with it so it's gotta be in your code somewhere. I'd suggest you show us at least you New Sub!
    That's good news. Probably something stupid on my end then...
    VB.NET Code:
    1. Public Sub New()
    2.         InitializeComponent()
    3.  
    4.         VirtualMode = True
    5.         Dim HexColumns(17) As DataGridViewTextBoxColumn
    6.         For i As Integer = 0 To 17
    7.             HexColumns(i) = New DataGridViewTextBoxColumn
    8.             With HexColumns(i)
    9.                 .HeaderText = Hex(i - 1)
    10.                 .Width = 25
    11.                 .SortMode = DataGridViewColumnSortMode.NotSortable
    12.                 .Name = .HeaderText
    13.             End With
    14.         Next
    15.         Columns.Clear()
    16.         Columns.AddRange(HexColumns)
    17.  
    18.         With Columns(0)
    19.             .HeaderText = "Addr"
    20.             .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
    21.             .Width = 75
    22.         End With
    23.  
    24.         With Columns(17)
    25.             .HeaderText = "Text"
    26.             .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
    27.             .Width = 150
    28.         End With
    29.     End Sub

  7. #7
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Inherited DataGridView Column Double Up

    This sounds like the same problem, but I don't understand the solution.
    Another link to another solution

    If I add "If Me.DesignMode Then" to the column creation section of the New() Sub, no columns get created at designtime or runtime.
    If I add "If Not Me.DesignMode Then" to the column creation section, columns get created twice.

    Interestingly, the extra columns don't have the alignment set as I set them in the New() Sub.

    First pic is design mode

    2nd is running the program. Note the alignment of the HeaderText...
    Attached Images Attached Images   
    Last edited by Meestor_X; Apr 25th, 2013 at 06:32 PM.

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Inherited DataGridView Column Double Up

    What happens if you delete InitializeComponent() from the New sub?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Inherited DataGridView Column Double Up

    Quote Originally Posted by dunfiddlin View Post
    What happens if you delete InitializeComponent() from the New sub?
    I get the warning:
    "Warning 1 'Public Sub New()' in designer-generated type 'AndysControls.HexDataGridView' should call InitializeComponent method."
    It also doesn't fix the problem.

    Here's another discussion of this problem.

    Did you have a look at the links I posted?

    You said earlier:
    Nope. If there was a problem with inheriting DataGridView then it's been fixed. I'm not getting any problems with it so it's gotta be in your code somewhere.
    Are you able to make an inherited DataGridView with programmatically-created columns that doesn't show the problem I and the OP are experiencing?
    If so, can you show me your code so I can see where I'm going wrong?
    Last edited by Meestor_X; Apr 25th, 2013 at 06:58 PM.

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Inherited DataGridView Column Double Up

    I can but I haven't worked it up to a full custom control, just as an additional class instanced at runtime. If you can zip up your control project and attach it here, I'll be happy to see what can be done.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  11. #11
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Inherited DataGridView Column Double Up

    Ok, got it sorted out. It sure was a difficult solution to find!

    Had to add:
    vb.net Code:
    1. <System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
    2.     Overloads ReadOnly Property Columns As DataGridViewColumnCollection
    3.         Get
    4.             Return MyBase.Columns
    5.         End Get
    6.     End Property
    In my class that inherits the DataGridView.
    Still the designer (re)creates all the columns, but at least it doesn't add them to the DataGridView.
    I'm not sure if there would be a way to use this attribute to also supress the duplication of the Columns in the designer, just to really completely correct this problem...

    WHEW!

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Inherited DataGridView Column Double Up

    put a break point on this line:
    Columns.Clear()
    BEFORE it runs... check to see what the Columns.Count is...

    then run that ONE line... go no further...
    before this line:
    Columns.AddRange(HexColumns)
    again check to see what the Columns.Count is... Something tells me that it hasn't changed... clearing the columns does NOT (if I remember right) REMOVE the columns... it jsut clears them out, sets the headers to empty, removes databindings, data, etc...

    It's like using Textbox.Clear... doesn't actually remove the textbox.. it just clears it out.

    If you want to remove the existing columns, then that's what you need to do... REMOVE the columns... THEN add the new ones.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Inherited DataGridView Column Double Up

    Please see the previous post AND all my links.

    The problem is the designer is creating the columns and adding them in addition to me programmatically adding the columns.
    My previous post shows the solution which works.

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Inherited DataGridView Column Double Up

    I did read all of the posts... and I keyed in on this:
    Still the designer (re)creates all the columns, but at least it doesn't add them to the DataGridView.

    which tells me the problem is still there but you've managed to gloss over it... fine, what ever. your project.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Inherited DataGridView Column Double Up

    Quote Originally Posted by techgnome View Post
    which tells me the problem is still there but you've managed to gloss over it... fine, what ever. your project.
    Ok, I'll bite.
    Your solution says to remove the columns and that Columns.Clear() does not do this. So what would actually REMOVE the columns then?
    If you look at the form1.designer file, you can see where it recreates all the columns as I was saying. The only way I've found to stop this is my solution, but I'm open to other suggestions...

Posting Permissions

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



Click Here to Expand Forum to Full Width