Link to home
Start Free TrialLog in
Avatar of fruhj
fruhj

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

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
SOLUTION
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
Avatar of fruhj
fruhj

ASKER

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.