Avatar of Bernard Thouin
Bernard Thouin
Flag for Switzerland asked on

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
Visual Basic.NET* DataGridView

Avatar of undefined
Last Comment
Bernard Thouin

8/22/2022 - Mon
ElrondCT

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.
ASKER CERTIFIED SOLUTION
Scott McDaniel (EE MVE )

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bernard Thouin

ASKER
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 ? ?
Scott McDaniel (EE MVE )

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Bernard Thouin

ASKER
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.
Scott McDaniel (EE MVE )

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

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.