We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


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

fruhj asked
Medium Priority
Last Modified: 2007-12-19
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
        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)
End Sub

Thanks in advance for your help on this!
Watch Question

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.


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
You can use the dataset designer to change this property on the datacolumn as well.


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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.