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

Posted on 2006-03-23
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!
Question by:fruhj
    LVL 34

    Accepted Solution

    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.

    LVL 16

    Assisted Solution

    You can use the dataset designer to change this property on the datacolumn as well.
    LVL 12

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now