[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-03-23
3
Medium Priority
?
524 Views
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
        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!
0
Comment
Question by:fruhj
3 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 1700 total points
ID: 16276131
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
 
LVL 16

Assisted Solution

by:RobertRFreeman
RobertRFreeman earned 300 total points
ID: 16276332
You can use the dataset designer to change this property on the datacolumn as well.
0
 
LVL 12

Author Comment

by:fruhj
ID: 16277202
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

872 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