Question DatagridView - Changing what triggers an add row event

robtyketto

Member
Joined
Jul 23, 2009
Messages
23
Programming Experience
Beginner
Greetings,

I have a datagridview which is populated from the SelectionChangeCommitted event of the combobox.

The datagridview contains both bound and unbound data columns (see below).
VB.NET:
                    'Bind specific columns ONLY
                    'Colums 0 AND 7 are unbound columns
                    DataGridView1.DataSource = bookingData.Tables("bookings")
                    DataGridView1.Columns(1).DataPropertyName = bookingData.Tables("bookings").Columns(0).ToString
                    DataGridView1.Columns(2).DataPropertyName = bookingData.Tables("bookings").Columns(1).ToString
                    DataGridView1.Columns(3).DataPropertyName = bookingData.Tables("bookings").Columns(2).ToString
                    DataGridView1.Columns(4).DataPropertyName = bookingData.Tables("bookings").Columns(3).ToString
                    DataGridView1.Columns(5).DataPropertyName = bookingData.Tables("bookings").Columns(4).ToString
                    DataGridView1.Columns(6).DataPropertyName = bookingData.Tables("bookings").Columns(5).ToString

There is a button on the form to allow add a row via
VB.NET:
DataGridView1.AllowUserToAddRows = True

Then I populate the new row with default values in the DataGridView1.DefaultValuesNeeded event.

VB.NET:
Private Sub dataGridView1_DefaultValuesNeeded(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) _
    Handles DataGridView1.DefaultValuesNeeded

        With e.Row
            .Cells("bookingDate").Value = New Date(Now.Year, Now.Month, Now.Day)
            .Cells("startTime").Value = New Date(Now.Year, Now.Month, Now.Day, 8, 0, 0)
            .Cells("endTime").Value = CType(e.Row.Cells("startTime").Value, DateTime).AddHours(1)
            .Cells("booker").Value = bookersName
            .Cells("phoneExt").Value = phoneNum
            .Cells("bookingsFrequency").Value = "1"
        End With

    End Sub

I didn't expect the behaviour of when ANY cell on the new row has a value added then a new row is created.

Ideally I would like to use the RowValidated/RowValidating event and when the user Leaves the row via TAB key then it would validate and create a new row ONLY if all the data was ok.

Is this the default behaviour? can this be changed i.e. not trigger creation of a new row if a cell is populated.

Thanks
Rob
 
Last edited:
I think you're missing the point of the AllowUserToAddRows property. When that's True then a data-entry row is provided at the bottom of the grid and the user can simply type into it to add a new row. If you want new rows only to be added in code then you do NOT want the user to add new rows, so AllowUserToAddRows should be False. It will then be up to you to add a new row to the grid in code if and when it's needed.
 
So far I have it when they click a button the allowusertoaddrows is set to true.

New row appears with default values (assume this is because I have BOTH bound and unbound data I can do this).

However, can I then turn the mode OFF directly afterwards?
So users can add ONE ROW at a time, as it would then be in edit mode they could add in values to the empty columns, those with values not supplied by default?

I wanted to then do the row validation when they click a button, however I think the row is added to the datatable immediately anyway (datagrid view has columns bound to a dataset)?

Then if data is valid the button would toggle to allow ANOTHER row to be added, is this plausable?
 
Last edited:
Back
Top