Question Insert a value in a DataGridview - value fails to insert

Fedaykin

Active member
Joined
Mar 20, 2013
Messages
30
Programming Experience
Beginner
I'm not able to figure out why this will not insert a value into the datagridview cell:

VB.NET:
        Dim SDateCol As New DataGridViewTextBoxColumn
        SDateCol.HeaderText = "StartDate"
        SDateCol.Name = "StartDate"
        DataGridView5.Columns.Insert(4, SDateCol)
        Dim cS As DataGridViewColumn = Me.DataGridView5.Columns("StartDate")
        cS.Width = 65


        For Each row As DataGridViewRow In DataGridView5.Rows
            row.Cells("StartDate").Value = "SomeValue"
        Next

I don't get an error and the cells are just blank. When I set a break during debug on the rows.Cells("StartDate").Value = "SomeValue" I can see that the line recognizes the value "SomeValue" for that line.

Stumped.

UPDATE:

Okay, after a day of messing with this on and off I've found that adding this exact same code to a button click event makes it work. But calling this code in a stand alone Sub it doesn't work. Can anyone tell me why?
 
Last edited:
There's no such thing as a "stand-alone Sub". Methods are methods. The Click event handler of a Button is a Sub like any other. If you originally had that code in a method that was not an event handler then you would have had to call that method from an event handler or another method that was called from event handler somewhere in the stack. If the code works then it would work no matter the method you executed it in, as long as it was executed on the UI thread. Either you simply never called the method that code was in or else you called it on a secondary thread somehow. That second option is possible but unlikely.

By the way, if you find a solution then please add a new post rather than adding to an existing post.
 
Thank you for your reply jmcilhinney I was beginning to lose hope.

Forgive my description of standalone. It was simply a descriptive word for 'separate'.

I originally included the code in a sub than handled TabPage3.Enter and that loaded the Grid and displayed the inserted column but failed to update the new column with values.

I tried calling a separate sub from the Tabpage3.Enter and got the same result.

When I tried it in a sub that handles a buttonclick the code worked. I tried calling the button click from the original Tabpage3.Enter and is doesn't work.

Logic does not seem to be in play here.
 
You don't call one event handler from another. Event handlers are executed when the event they're handling is raised. If you want to execute the same code on two different events then you either use a single method to handle both events or you put the code in a separate method and then call that method from both event handlers.

You should not be handling the Enter event of a TabPage. If you want to do something when the user selects a specific tab then handle the SelectedIndexChanged event of the TabControl and test which TabPage is selected.
 
I respectfully disagree with not be able to handle an Enter event of a Tabpage. The Event is there and available to be handled, the fact that it does not work is a fault in Visual Studio, not in the use of it.

Using SelectedIndexChanged is a nice work around, thank you for the suggestion. Another thing I just discovered is that using TabControl1.SelectedTab = TabPage3 before running the code is also a viable work around as well (event though per the Event Tabpage3.Enter the Tab was already entered by the user and therefore 'selected'.

I'm happy to say that I have not run into many things in Visual Studio that just flat don't work as advertised, so I'm not going to get all bent out of shape about it.

I really do appreciate your input.
 
I respectfully disagree with not be able to handle an Enter event of a Tabpage. The Event is there and available to be handled, the fact that it does not work is a fault in Visual Studio, not in the use of it.
The fact that an event exists does not necessarily mean that you should handle it. Remember that Enter is actually a member of the Control class, which is the base for all controls. Every control is specialised in some way and that specialisation makes many inherited members useless or inappropriate in many controls, e.g. the Text property of the NumericUpDown control.
 
Back
Top