How Do I set defaults for a cell in a datagridview in VB.net 2.0 winforms?

Hi Experts.

  I have some examples from a book (VB2005 DB Programming - wrox press) I'm trying to follow.

  I have a Datagridview on a form.

  on the top of the form is the standard first, prev, counter, next, last navigation buttons
  next to them are buttons to add and delete a record (and of course the save button)

  I need to default one field in the table when a new record is added.

 I added the following procedures: (per page 150-151)

Private Sub UsersDataGridView_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles UsersDataGridView.DefaultValuesNeeded
        setdefaults(e.Row)
        MsgBox("in firstsub")
End Sub

Private Sub setdefaults(ByVal rowadded As DataGridViewRow)
        rowadded.Cells("DataGridViewTextBoxColumn8").Value = "EGL"
        MsgBox("insecond sub")
End Sub

The above code works great if I click anywhere in the row indicated by the >* symbol on the left - both msgboxes display and my value is added.
However it doesn't fire if I click the yellow  + Icon on the navigation toolstrip.

I tried this but it didn't work:
Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Dim x As System.Windows.Forms.DataGridViewRowEventArgs
        x = CType(e, System.Windows.Forms.DataGridViewRowEventArgs)
        setdefaults(x.Row)
End Sub


Thanks in advance for your help on this!
LVL 12
fruhjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SanclerCommented:
My recommendation would be a different approach entirely.  That is, to set the .DefaultValue property of the relevant column in the source datatable to "EGL".  The code to do that, which would go immediately after you have filled the dataset, would be on the lines of

        Me.MyDataSet.MyTable.Columns(MyColumnName).DefaultValue = "ELG"

You could then dispense with both the subs, and it should work whether you create the new row with the "*" in the datagrid or the "+" on the BindingNavigator.

Roger
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobertRFreemanCommented:
You can use the dataset designer to change this property on the datacolumn as well.
0
fruhjAuthor Commented:
Thanks so much to both of you.

This was exactly the kind of answer I was looking for - seemed there had to be an easier way.

I had actually been in the dataset designer earlier today looking for that, but didn't see any options.

Turns out I was right clicking on the field name, and apparently the context menu I needed only appears when you click to the left of the field name in that little area.

I'm going to give most of the points to Sancler - since his post set me on the right path (I did all my testing and got it working with his post - just saw Roberts now by clicking refresh)
I am awarding Robert some points too, - for pointing out what I had missed earlier in the day.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.