VB.Net datagridview, need to set some columns automatically when user inserts a new row

Hi

I have developed a number of console apps, but this is my first Windows forms-based app, with a DataGridView control (which is showing the data in a datatable) where users can add rows. When they insert a new row, I want to set some columns of that row automatically (insertion date/time, user name, that kind of stuff).

I don't knw how to do that... Can you help ?

Thanks
Bernard
LVL 1
bthouinIT Analyst and developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ElrondCTCommented:
I use the .CurrentCellChanged event handler (blnLoading is set at the start of the Load event and turned off at the end of Load, so if the routine is triggered during that time, it's simply ignored):

    
Private Sub dgvChoices_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvChoices.CurrentCellChanged
        If blnLoading = False AndAlso Not dgvChoices.CurrentCell Is Nothing Then   
            Dim drChoice As dsAN.ChoicesRow = DirectCast(dvwChoices(dgvChoices.CurrentRow.Index).Row, dsAN.ChoicesRow)
            If drChoice.IsfldDateNull Then
                drChoice.fldDate = Date.Today
            End If
            If drChoice.IsfldChoiceNull Then
                drChoice.fldChoice = ""
            End If
        End If
    End Sub

Open in new window


The key here is that at least one of the fields in the newly created row is null, or in some other way it's evident that the row is new and not properly initialized. If you get a match on that when .CurrentCellChanged, use that as the flag to change the other fields.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
If you need to do this ONLY when the user is in the "new row", you can check the RowEntered event and determine if they're on a new row:

if e.RowIndex = YourDataGridView.NewRowIndex Then
  >> modify your row cells here:
  YourDataGridView.Rows(e.RowIndex).Cells("YourCellName").Value = SomeValue
End If

You could also set the DefaultCellStyle.NullValue property of your DGV column:

YourDataGridView.Columns(ColIndex).DefaultCellStyle.NullValue = SomeValue

If you're using a Datatable to fill the grid, you can also set the Default Value of a DAtaColumn.

Finally, there's the DefaultVlauesNeeded event for your DGV. You can set Default Values for New Rows there:

Private Sub YourDataGridView_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles YourDataGridView.DefaultValuesNeeded
        e.Row.Cells("YourCellName ").Value = "SomeValue"
    End Sub
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
bthouinIT Analyst and developerAuthor Commented:
Hi Scott

Thanks a lot, it works fine.

Just one thing I first stumbled on: why is the cell name = <datatable column name> & "DataGridViewTextBoxColumn", instead of just  being the datatable column name ? ?
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
If you "autobind" a DataGridView to a Datatable, the DGV cells should be named the same as your DataTable columns. For example, if my datatable has a column named "CustomerName", by DGV should contain a column named "CustomerName" as well.
0
bthouinIT Analyst and developerAuthor Commented:
Well I defined a dataset, linked it to the form in my app, then added the grid view and set it datasource to be one of the datatables in the dataset, I don't know if that is what you call autobinding, but the result is that the column names are starting with the "real" column name but then this "DataGridViewTextBoxColumn" gets appended to it.

I found out because I got an error telling me the column name <datatable column name> could not be found in the <MyGrid>.Rows(e.RowIndex).Cells("<datatable colum name>") statement, so I debugged and printed out the used column name.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
That's odd, and I'm not sure why that would occur. When I bind my DGV to a Datatable, the columns names are exactly the same as those of the Datatable.

I don't generally use Datasets, however. I'm curious if that's the reason.
0
bthouinIT Analyst and developerAuthor Commented:
I'm using VS2013, maybe that also makes a difference ?

Anyway, it works fine that's the main thing, you get the points, thanks a lot :-)

Bernard
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.