Results 1 to 12 of 12

Thread: [RESOLVED] Passing DataGridView Name/Control Through Subroutines

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Location
    Toronto
    Posts
    8

    Resolved [RESOLVED] Passing DataGridView Name/Control Through Subroutines

    Hi, I'm new to VBForums. I'm trying to re-write an access program in Visual Basic.

    I would like to have a generic sub-routine to format a datagridview but I cant seem to get the name of the dgv to the formatting sub (dgvLnFlowFmt).
    I excluded LoadGrid() as this was not a problem. Everything ran fine when referencing the dgv by name directly.

    I seem to be setting the Control to DataGridView as opposed to the actual dgv I would like to format. ANy help would be greatly appreciated.

    Sorry if this was not posted properly!

    Code:
     Public Sub CouponFlows()
    
             sqlQuery = "SELECT * "
    
            LoadGrid(sqlQuery)
            dgvLnFlowFmt(DataGridView1) 'trying to pass "DataGridView1" name to dgvLnFlowFmt
    
    
    End Sub
    
    
    Sub dgvLnFlowFmt(ByRef dgvSelect As DataGridView)
    
     
            'Format DataGridView
            'Set property values appropriate for read-only display and limited interactivity. 
            dgvSelect.AllowUserToAddRows = False
            dgvSelect.AllowUserToDeleteRows = False
            dgvSelect.AllowUserToOrderColumns = True
            dgvSelect.ReadOnly = True
            dgvSelect.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            dgvSelect.MultiSelect = False
            dgvSelect.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
            dgvSelect.AllowUserToResizeColumns = False
            dgvSelect.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
            dgvSelect.AllowUserToResizeRows = False
            dgvSelect.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
    
            'Set the selection background color for all the cells.
            dgvSelect.DefaultCellStyle.SelectionBackColor = Color.LightGreen
            dgvSelect.DefaultCellStyle.SelectionForeColor = Color.Blue
    
            dgvSelect.GridColor = Color.LightGray
            dgvSelect.BorderStyle = BorderStyle.None
            dgvSelect.ForeColor = Color.Black
    
      
            'Sets default row color and alternating rows different color
            dgvSelect.RowsDefaultCellStyle.BackColor = Color.White
            dgvSelect.AlternatingRowsDefaultCellStyle.BackColor = Color.Bisque
    
            'Remove Row/Column header
            dgvSelect.RowHeadersVisible = False
            dgvSelect.ColumnHeadersVisible = False
    
            'resizes columns to largest width for each column
             dgvSelect.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            dgvSelect.DefaultCellStyle.Padding = New Padding(3, 0, 3, 0)
            dgvSelect.Columns(0).MinimumWidth = 80
            dgvSelect.Columns(2).DefaultCellStyle.Format = "dd'/'MM'/'yyyy"
            dgvSelect.Columns(1).MinimumWidth = 81
            dgvSelect.Columns(2).DefaultCellStyle.Format = "dd'/'MM'/'yyyy"
            dgvSelect.Columns(2).MinimumWidth = 101
            dgvSelect.Columns(2).DefaultCellStyle.Format = "c"
            dgvSelect.Columns(3).MinimumWidth = 81
            dgvSelect.Columns(4).MinimumWidth = 101
            dgvSelect.Columns(4).DefaultCellStyle.Format = "c"
            dgvSelect.Columns(5).MinimumWidth = 81
    
    
    End Sub
    Last edited by Shaggy Hiker; Sep 19th, 2017 at 04:27 PM. Reason: Added CODE tags

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Passing DataGridView Name/Control Through Subroutines

    It's mostly correct. I edited the post to add [CODE][/CODE] tags. You can do this by pressing the # button and pasting the code between the tags, or paste in the code, then highlight it and press the # button.

    Also, I have some doubts that this is in the right forum. I believe that the DGV is a .NET control, rather than a classic VB control, but I'm not certain, so I've left it, for now. Which version of VB is this?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Location
    Toronto
    Posts
    8

    Re: Passing DataGridView Name/Control Through Subroutines

    Thanks...
    Using Visual Basic 2010 Express

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Passing DataGridView Name/Control Through Subroutines

    Ok, that's a version of .NET, so I have moved the thread. Everything from 2002 on is .NET, though the .NET name was officially dropped around 2005.
    My usual boring signature: Nothing

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Passing DataGridView Name/Control Through Subroutines

    You are passing the datagridview control to the method, and from what I see it's what you should be doing. You pass the DGV in, it gets formatted and you move on. Why do you want to pass only the DGV name?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Passing DataGridView Name/Control Through Subroutines

    I agree with kebo that the code is basically correct as it is. The one thing you should change is declare the parameter ByVal rather than ByRef. The only reason to declare it ByRef is if you wanted to assign a different DataGridView object to the parameter inside the method and have that change affect the original variable passed in. You're not doing that so declaring the parameter ByRef serves no useful purpose. ByVal is the default for a reason and you should use it here.

    As for your suggestion that you want to pass in a String containing the name of the DataGridView, why would you want to do that? The point of the method is to format a DataGridView so the only useful thing you could do with the name would be to use it to get the actual DataGridView with that name. You already have that DataGridView though and it's what you're currently passing into the method, so why would you want to make things more complicated for the same result? The parameter is declared as type DataGridView so you must pass a DataGridView. If you wanted to pass in a String then you'd have to declare the parameter as type String, but then you'd have to add code to the method to use that name to get back the same DataGridView object that you already have.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Location
    Toronto
    Posts
    8

    Re: Passing DataGridView Name/Control Through Subroutines

    Thanks Kebo/jmcilhinney!

    I have dgvs on different tabs and the dgv which I set with "dgvLnFlowFmt(DataGridView1)" does not seem to be getting through. My format is not changing according to my settings. I think I left the tab part out above.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Passing DataGridView Name/Control Through Subroutines

    If you are talking about a TabControl then the issue may be the fact that controls on a TabPage are not loaded until that TabPage is selected. If you call that method and pass a grid that is on a TabPage that has not yet been displayed then nothing you do in that method will make a difference. In that case, your options are to wait until the user selects that tab to configure the grid on that page or select that TabPage in code at startup so you can configure the grid then.

  9. #9
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: Passing DataGridView Name/Control Through Subroutines

    Quote Originally Posted by ..MDB View Post
    Hi, I'm new to VBForums. I'm trying to re-write an access program in Visual Basic.

    I would like to have a generic sub-routine to format a datagridview but I cant seem to get the name of the dgv to the formatting sub (dgvLnFlowFmt).
    I excluded LoadGrid() as this was not a problem. Everything ran fine when referencing the dgv by name directly.

    I seem to be setting the Control to DataGridView as opposed to the actual dgv I would like to format. ANy help would be greatly appreciated.

    Sorry if this was not posted properly!

    Code:
     Public Sub CouponFlows()
    
             sqlQuery = "SELECT * "
    
            LoadGrid(sqlQuery)
            dgvLnFlowFmt(DataGridView1) 'trying to pass "DataGridView1" name to dgvLnFlowFmt
    
    
    End Sub
    
    
    Sub dgvLnFlowFmt(ByRef dgvSelect As DataGridView)
    
     
            'Format DataGridView
            'Set property values appropriate for read-only display and limited interactivity. 
            dgvSelect.AllowUserToAddRows = False
            dgvSelect.AllowUserToDeleteRows = False
            dgvSelect.AllowUserToOrderColumns = True
            dgvSelect.ReadOnly = True
            dgvSelect.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            dgvSelect.MultiSelect = False
            dgvSelect.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
            dgvSelect.AllowUserToResizeColumns = False
            dgvSelect.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
            dgvSelect.AllowUserToResizeRows = False
            dgvSelect.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
    
            'Set the selection background color for all the cells.
            dgvSelect.DefaultCellStyle.SelectionBackColor = Color.LightGreen
            dgvSelect.DefaultCellStyle.SelectionForeColor = Color.Blue
    
            dgvSelect.GridColor = Color.LightGray
            dgvSelect.BorderStyle = BorderStyle.None
            dgvSelect.ForeColor = Color.Black
    
      
            'Sets default row color and alternating rows different color
            dgvSelect.RowsDefaultCellStyle.BackColor = Color.White
            dgvSelect.AlternatingRowsDefaultCellStyle.BackColor = Color.Bisque
    
            'Remove Row/Column header
            dgvSelect.RowHeadersVisible = False
            dgvSelect.ColumnHeadersVisible = False
    
            'resizes columns to largest width for each column
             dgvSelect.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            dgvSelect.DefaultCellStyle.Padding = New Padding(3, 0, 3, 0)
            dgvSelect.Columns(0).MinimumWidth = 80
            dgvSelect.Columns(2).DefaultCellStyle.Format = "dd'/'MM'/'yyyy"
            dgvSelect.Columns(1).MinimumWidth = 81
            dgvSelect.Columns(2).DefaultCellStyle.Format = "dd'/'MM'/'yyyy"
            dgvSelect.Columns(2).MinimumWidth = 101
            dgvSelect.Columns(2).DefaultCellStyle.Format = "c"
            dgvSelect.Columns(3).MinimumWidth = 81
            dgvSelect.Columns(4).MinimumWidth = 101
            dgvSelect.Columns(4).DefaultCellStyle.Format = "c"
            dgvSelect.Columns(5).MinimumWidth = 81
    
    
    End Sub
    Hi mdb,

    I like to use Public Subs or evan make a Class with Formating functions for the DGV

    here a sample for Columnsrezise you can Drop on your Form.
    Code:
     Public Sub DGVColumsAutoSize(ByVal myDGV As DataGridView, ByVal AutoSize As Boolean)
            If myDGV Is Nothing Then
                Exit Sub
            End If
            Dim ColumnMode As DataGridViewAutoSizeColumnMode
            If AutoSize Then
                ColumnMode = DataGridViewAutoSizeColumnMode.AllCells
            Else
                ColumnMode = DataGridViewAutoSizeColumnMode.None
            End If
            With myDGV
                For i As Integer = 0 To .Columns.Count - 1
                    .Columns(i).AutoSizeMode = ColumnMode
                Next
            End With
        End Sub
    you call it then where(when you need it.

    Code:
    'in Form Load if you want
       DGVColumsAutoSize(DataGridView1, True)
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Location
    Toronto
    Posts
    8

    Re: Passing DataGridView Name/Control Through Subroutines

    Thanks, I wanted to load several tabs with appropriate data at the time I retrieve the data. This would be important for speed as well as not having to re-check data every time the tab is selected. This section is read only for the user.

    I quickly tested Chris' post and it seems to work. I will adjust for the formatting I need and re-test.

    Thanks.

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Location
    Toronto
    Posts
    8

    Re: Passing DataGridView Name/Control Through Subroutines

    Thanks Chris,

    I will re-test with my formatting and confirm.

    Best,
    MDB

  12. #12

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Location
    Toronto
    Posts
    8

    Re: [RESOLVED] Passing DataGridView Name/Control Through Subroutines

    Thanks all. Suggestions worked

    Also had to change .minimumwidth to width for formatting a fixed column width

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