Rowsadded event doesn't fire for all rows

dawatson

Member
Joined
Sep 15, 2008
Messages
18
Programming Experience
10+
I'm using a datagridview in vb.net 2008 that is bound to a bindingsource.
In the form load event, I have tableadapter.fill method to load the data.
When the fill method is executed, the rowsadded event for the grid also fires.

However, the event doesn't fire for all rows returned from the fill method.
I've checked the bindingsource.count property to verify the number of records.
The correct number of rows are displayed in the grid, but the event doesn't fire for all the rows.
For example, there may be 4 rows displayed in the grid, but the rowsadded event only fires 2 times.

There are several calculated columns in the grid, but the columns are only calculated for the
number of rows that equal the number of times the rowsadded event is fired.
The other rows are empty for the calculated columns.

Does the rowsadded event always fire for each row?

Thanks,
DW
 
Hi Tom,

Sorry for the delay in responding. I've been swamped with other work.
Yes, if rows are added the rowsadded event fires.
It doesn't fire for modified records. I wouldn't think that it should.

DW
 
See this : DataGridViewRowsAddedEventArgs Members (System.Windows.Forms)

The event argument you get in that event handler has the index of the first added row and the number of rows added. In the docs for the event, it says when you add the rows programmatically, it is normal for the event to be fired less times than the number of added rows because it considers the adding operation of many rows as a single event.

Or so I understand, it is not very clear how many times the event should fire. To stay on the safe side, I would not presume anything about the number of times it is fired. Just that for every row, there was one event fired although many row additions may have been grouped in the same event (possibly for efficiency reasons or to support shared rows).
 
If I understand what you are say correctly, it is working properly.

When you call the adapter.fill(dataset.table) method, only new records that were added would cause the rowsadded event to fire. Existing records will remain unchanged.

Here is an example snippet that will show the row state (unchanged, modified, deleted, added)

VB.NET:
        Dim strResults As String = ""

        For Each row As DataRow In DataSet.Tables("TableName_Or_Index")
            strResults &= "Record : " & vbTab _
                       & row.RowState.ToString & vbCrLf
        Next

        MsgBox(strResults)
 
Does the rowsadded event always fire for each row?

Thanks,
DW

No. It's called event coalescing and it would be very tedious and processor intensive to fire events every time one of thousands of trivial actions occurs.. In fact, methods (BeginLoadData/EndLoadData) exist to turn off event firing entirely, as it just slows everything down
 
I figured there was a performance reason, but I didn't know the name of this technique! thanks :)
 
the rowsadded is fired everytime a row is written onto your grid. that means while the fill does what it usually does, each record read from your database is inserted into your grid. you can check this by placing a msgbox(grid.count) to show how many rows have been written onto your grid.

maybe the problem you are really facing is the display of those calculated values in their corresponding cells. you might as well try checking for null values for it may be a key to your problem.
 
so.. which rowadding evt you are talking abt?
if u r taking abt grid_rowadding ,it only works on user row added time.
and the datatable_rowadding evt always work when the fill method is executed and rows start adding..
but i suggest u to work with bindingSource and its adding evt
 
Back
Top