Link to home
Start Free TrialLog in
Avatar of DavidGreenfield
DavidGreenfieldFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Runing code when form is loading does not work

Hi there

I want to be able to put a link label onto a datagrid.

The following works on a loaded form:

        Dim ColumnToChange As Integer = 2
        Dim CurrentValue As String

        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            CurrentValue = DataGridView1(ColumnToChange, i).Value
            DataGridView1(ColumnToChange, i) = New DataGridViewLinkCell
            DataGridView1(ColumnToChange, i).Value = CurrentValue
        Next

But when it is run on the on_load event or the activated event it runs through the code - but does not display the link label on the grid.

How can I get around this?

many thanks in advance
Avatar of Manish Chhetia
Manish Chhetia
Flag of India image


use the InitializeComponent event of the form (in the Designer of the Form)
it will work surely and also the way u want

Try It
Avatar of Kinger247
Kinger247

Hi  DavidGreenfield, put the code in your Form1_Activated event:

    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Dim ColumnToChange As Integer = 2
        Dim CurrentValue As String

        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            CurrentValue = DataGridView1(ColumnToChange, i).Value
            DataGridView1(ColumnToChange, i) = New DataGridViewLinkCell
            DataGridView1(ColumnToChange, i).Value = CurrentValue
        Next
    End Sub
Avatar of DavidGreenfield

ASKER

I did try the code in the activated event and it still doesn't work.  If i run it from a button click it works though.

For most datagridviews I am showing it doesn't matter - but this one is a static grid that the user can immediately view when loading the screen

thanks for your help

What about the InitializeComponent event (write ur code in the end of this event)

Try . it will work 4 u
Hi there manch

I did put it in the initializecomponent event and that didn't work either, I can't seem to tell if it is being run or not though

Should I be putting more than just the link label code in there?
This must mean that the data is still being loaded after these events ?
Data takes about 5 seconds to load so possible.

Is there anything I can do to hold off adding a link label until the datagrid has finished loading?
I have managed to get around this,

Basically by calling the code as below works:

Public Sub test(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load

I was calling it in (and lots of combinations based on):

Private Sub Form(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

I hope this will help someone else out too!
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial