Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

With Command Looping 2

Status
Not open for further replies.

kennedymr2

Programmer
May 23, 2001
594
AU

I have a form with 3 datagridView's ie DataGridView1 DataGridView2 etc

With DataGridView1
.GridColor = Color.Red
.CellBorderStyle = DataGridViewCellBorderStyle.None
.BackgroundColor = Color.LightGray

'.DefaultCellStyle.SelectionBackColor = Color.Red
.DefaultCellStyle.SelectionForeColor = Color.Yellow

.DefaultCellStyle.WrapMode = DataGridViewTriState.[True]

.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.AllowUserToResizeColumns = False

.RowsDefaultCellStyle.BackColor = Color.Bisque
.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
end with

With DataGridView2
.as above
end with
With DataGridView3
.as above
end with

>>???? is there a way of looping through without having to duplicate the code for each datagridview

eg

for ll= 1 to 3

if ll=1 then
with datagridview1
endif


if ll=2 then
with datagridview2
endif

etc

next ll


Appreciate any advice

Kennedymr2

 
Hi.
One thing that crossed my mind is iterating through all controls in the form.
Check if one control is DataGridView, then play something about it.
For example:

Code:
For Each ctr As Control In Me.Controls
    If TypeOf ctr Is DataGridView Then
       With ctr
            ....
       End With
    End If
Next

This procedure might get slower if the form has many controls in it.
One thing to be aware is that the container of the DataGridView is the form itself. Not in other container such as Panel.

Hope this helps.
Regards,
 
I'd write a subroutine to perform the formatting and pass a reference to each DataGridView as a parameter:

Code:
Private Sub FormatDataGridView(ByRef dgv As DataGridView)
    With dgv
        .GridColor = Color.Red
        .CellBorderStyle = DataGridViewCellBorderStyle.None
        .BackgroundColor = Color.LightGray

        .DefaultCellStyle.SelectionForeColor = Color.Yellow

        .DefaultCellStyle.WrapMode = DataGridViewTriState.True

        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .AllowUserToResizeColumns = False

        .RowsDefaultCellStyle.BackColor = Color.Bisque
        .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
    End With
End Sub

Then explicitly call the subroutine for each DataGridView you want to format:

Code:
FormatDataGridView(DataGridView1)
FormatDataGridView(DataGridView2)
FormatDataGridView(DataGridView3)

You could also call this subroutine within the body of the For Each loop as shown by mansii
 
FormatDataGridView(DataGridView1)
FormatDataGridView(DataGridView2)
FormatDataGridView(DataGridView3)

Private Sub FormatDataGridView(ByRef dgv As DataGridView)

When i call the sub it comes up with object error

Don't seem to be able to work out how to pass byref as datagridview with FormatDataGridView(DataGridView1)

Appreciate if you could help in how to call the sub


Regards
 
Sorry... Its works fine ......my datagrid had a different name !!!!!
 
In addition, you can combine Dave's code with mine:
Code:
For Each ctr As Control In Me.Controls
    If TypeOf ctr Is DataGridView Then
       With ctr
            FormatDataGridView(ctr) 
       End With
    End If
Next

Regards
 
[tt]With[/tt] statement not needed here:

Code:
For Each ctr As Control In Me.Controls
    If TypeOf ctr Is DataGridView Then
       [s]With ctr[/s]
       FormatDataGridView(ctr) 
       [s]End With[/s]
    End If
Next

Just being picky... :)

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top