Results 1 to 11 of 11

Thread: User Control

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    User Control

    I created a user control with few labels, buttons and a dataGridView. I add the control on the fly and based on some conditions I apply different styles to data row.
    1 ) styling works when i test on a form directly, however it doesn't when applying it in the user control
    2 ) is it possible to hide the cell at the row level ?

    Code:
    dim uc as new userControl 
    me.controls.add(uc)

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

    Re: User Control

    A DataGridView is a DataGridView so there's no reason that anything that works with a grid on the form shouldn't work with a grid on the user control. I'll wager that you're not using the correct reference or something like that. Can you show us exactly what you're doing in each case, i.e. what works with the form and what doesn't work with the user control?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: User Control

    i add the user control multiple times on the main form, would there be a referrencing issue the way im doing it ?
    in the usercontrol constructor i set the grid data source and then check rows data to apply cell styling
    here is a sample example, it display correct data on all added user controls
    Code:
    sub new()
    with grdView
    .autogeneratecolumns=false
    .datasource=---
    .columns(0).datapropertyname="..."
    .
    .
    .
    end with
    
    dim n as integer=0
    for each orow as datarow in table
    if orow(1)="I"
     grdView.rows(n).DefaultCellStyle=oStyle1
    
    end if
    n+=1
    next
    
    end sub

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

    Re: User Control

    Hmmm... nothing obvious there. Mind you, you obviously haven't posted the actual code, which I'm never a fan of. More than once I've seen people post what they think they have rather than what they actually have. Have you stepped through that code to see that it actually does what you expect?

    By the way, shouldn't that loop be a For loop rather than For Each. Why keep a separate loop counter when a For loop inherently keeps a loop counter? Just use the loop counter to index both collections. Alternatively, use a For Each loop to go through the grid rows and use the DataBoundItem to access the bound data, negating the need for a loop counter altogether.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: User Control

    here is the code on the main form

    Code:
    Private Sub Screen_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            loadCards()
        End Sub
    
     Sub loadCards(Optional nNo As Integer = 1)
            Dim strServed As String = "served" & nStationNo.ToString.Trim
            cmdstr = String.Format("select * from ordhdr where batchNo={0} and {1}=0 order by id desc", oCom.batchNo, strServed)
            If oCom.sqlfill(cmdstr, "c_service") > 0 Then
                For Each orow As DataRow In oCom.ds.Tables("c_service").Rows
                    Dim ucCard1 As New ucCard(orow("id"), orow("ordDt"), orow("Num")) With {.Name = "uc-" & orow("id")}
                    Me.Controls.Add(ucCard1)
                Next
             End If
        End Sub
    and this is the code the User control :

    Code:
     Sub New(nordId As Integer, dt As DateTime, ordNum As Integer)
            ' This call is required by the designer.
            InitializeComponent()
    
           
    
            ingCellStyle = New DataGridViewCellStyle
            With ingCellStyle
                .ForeColor = Color.Red
                .BackColor = Color.AliceBlue
                .Font = New Font("Arial", 12.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                .Alignment = DataGridViewContentAlignment.TopCenter
            End With
            loadDetails()
        End Sub
    	
    	
    	Sub loadDetails()
           
            With Grd
                .AutoGenerateColumns = False
                .DataSource = oCom.ds.Tables(ctable)
                .Columns(0).DataPropertyName = "sort"
                .Columns(1).DataPropertyName = "Desc"
            End With
    
            ' Restyle
            '=========
            Dim i As Integer = 0
            For Each orow As DataRow In oCom.ds.Tables(ctable).Rows
                If orow("Type") = "I" Then
                    'MsgBox(orow("itemdesc"))
                    ' Grd.Rows(i).DefaultCellStyle = ingCellStyle
                    Try
                        Me.Grd.Rows(i).DefaultCellStyle.ApplyStyle(ingCellStyle)
                    Catch ex As Exception
                        MsgBox(ex.ToString)
                    End Try
    
                End If
                i += 1
            Next
        End Sub

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

    Re: User Control

    Er ... the New Sub contains a predefined style and there doesn't appear to be any facility to change it. The loadDetails Sub (is it intended to be Public?) has no parameters so I can't see where exactly you think style changes could be introduced.
    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!

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: User Control

    ingCellStyle is declared globally, i set its property in the New() sub. then on the loadDetails i apply this style to rows that pass the conditon. I have had a similar issue before, where i was loading multiple Tabs with a GridView on each tab, evenThough i'd called the restyling on the load event, only the focused Tab seemed to display the changes. The work around was to call the Restyling method on the Tab.selectedIndex change.

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

    Re: User Control

    vb.net Code:
    1. Sub New(nordId As Integer, dt As DateTime, ordNum As Integer) ' none of these parameters are ever used!
    2.         ' This call is required by the designer.
    3.         InitializeComponent()
    4.  
    5.        
    6.  
    7.         ingCellStyle = New DataGridViewCellStyle
    8.         With ingCellStyle
    9.             .ForeColor = Color.Red
    10.             .BackColor = Color.AliceBlue
    11.             .Font = New Font("Arial", 12.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    12.             .Alignment = DataGridViewContentAlignment.TopCenter
    13.         End With
    14.         loadDetails()
    15.     End Sub
    16.    
    17.    
    18.     Sub loadDetails()
    19.        
    20.         With Grd
    21.             .AutoGenerateColumns = False
    22.             .DataSource = oCom.ds.Tables(ctable) ' this is a specific external datasource? Does it even exist at design time?
    23.             .Columns(0).DataPropertyName = "sort"
    24.             .Columns(1).DataPropertyName = "Desc"
    25.         End With
    26.  
    27.         ' Restyle
    28.         '=========
    29.         Dim i As Integer = 0
    30.         For Each orow As DataRow In oCom.ds.Tables(ctable).Rows ' why are we back to the table if it's already the datasource?
    31.             If orow("Type") = "I" Then ' I assume this means something
    32. ' are these lines operative or not? You have them commented out.
    33.                 'MsgBox(orow("itemdesc"))
    34.                 ' Grd.Rows(i).DefaultCellStyle = ingCellStyle
    35.                 Try
    36.                     Me.Grd.Rows(i).DefaultCellStyle.ApplyStyle(ingCellStyle)
    37.                 Catch ex As Exception
    38.                     MsgBox(ex.ToString) ' what's the point of this? There's nothing anyone can do about it!
    39.                 End Try
    40.  
    41.             End If
    42.             i += 1
    43.         Next
    44.  
    45. ' So we've been through this once for the initialisation. What happens if a new row is added or the pertinent value changed?
    46.     End 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

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: User Control

    vb.net Code:
    1. Sub New(nordId As Integer, dt As DateTime, ordNum As Integer) ' none of these parameters are ever used!
    2.         ' This call is required by the designer.
    3.         InitializeComponent()
    4.  
    5.        '' --- >>> [ i actually use the parameters here and load the datatable cTable used below, i took out to simpllify as it nor relevant to my question]
    6.  
    7.         ingCellStyle = New DataGridViewCellStyle
    8.         With ingCellStyle
    9.             .ForeColor = Color.Red
    10.             .BackColor = Color.AliceBlue
    11.             .Font = New Font("Arial", 12.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    12.             .Alignment = DataGridViewContentAlignment.TopCenter
    13.         End With
    14.         loadDetails()
    15.     End Sub
    16.    
    17.    
    18.     Sub loadDetails()
    19.        
    20.         With Grd
    21.             .AutoGenerateColumns = False
    22.             .DataSource = oCom.ds.Tables(ctable) ' this is a specific external datasource? Does it even exist at design time? : --- >>> [ create in the NEW()]
    23.             .Columns(0).DataPropertyName = "sort"
    24.             .Columns(1).DataPropertyName = "Desc"
    25.         End With
    26.  
    27.         ' Restyle
    28.         '=========
    29.         Dim i As Integer = 0
    30.         For Each orow As DataRow In oCom.ds.Tables(ctable).Rows ' why are we back to the table if it's already the datasource?
    31.             If orow("Type") = "I" Then ' I assume this means something
    32. ' are these lines operative or not? You have them commented out.
    33.                 'MsgBox(orow("itemdesc"))   --------------- [ not operative, just for testing to confirm it satisfies the condition]
    34.                 ' Grd.Rows(i).DefaultCellStyle = ingCellStyle
    35.                 Try
    36.                     Me.Grd.Rows(i).DefaultCellStyle.ApplyStyle(ingCellStyle)
    37.                 Catch ex As Exception
    38.                     MsgBox(ex.ToString) ' what's the point of this? There's nothing anyone can do about it! --- >>> [ Good point, again just for testing as vs2010 (on 64bit) does not report errors when debuging]
    39.                 End Try
    40.  
    41.             End If
    42.             i += 1
    43.         Next
    44.  
    45. ' So we've been through this once for the initialisation. What happens if a new row is added or the pertinent value changed? -- >>> [ once loaded, its static now data will be added]
    46.     End Sub
    Thanks

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

    Re: User Control

    [ i actually use the parameters here and load the datatable cTable used below, i took out to simpllify as it nor relevant to my question]
    You may not consider it relevant to the question but it is extremely relevant to our understanding of the Class. Do you think you could zip up your project in full and attach it here so that we can see it in action?
    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

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: User Control

    The work around was to reapply the style from parents not within the user control. attached is a sample

    Thanks gents
    Attached Files Attached Files

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