How do I know when the data binding is actually ready for the DataGridView?

I am very frustrated with this DataGridView control. What I really want to do is just make sure the grid is sorted by default first (although the user can sort it differently if necessary).

I tried to assign the DataSource and then call the Sort method. Sometimes it works, but sometimes it throws an InvalidOperationException. Debugging seems to indicate that the time it is down, the grid has no rows or columns. The choppy behavior made me guess that a race is happening, so maybe the grid is not snapping synchronously when I assign the datasource, but rather doing it in the background.

So I look at the MSDN doc and I find the DataBindingCompleted event there. It looks something like this, and I am attaching a handler. The event is being declared on the grid itself, so you think this means that all data bindings for the entire control are complete. However, it fires many times (I don't know, but I'm guessing once per cell) every time I actually bind new data to the grid.

I suppose I can live with it; the overhead is probably not huge, as the grid is apparently smart enough not to do anything at all if asked to sort "col1 asc" and is already sorted that way. (If not, I could always run this test myself and not call the sort, unless I intend to change something). However, I would really like to know if it is really impossible to know when the data binding is actually complete.

+3


source to share


1 answer


We typically use the DataBound event when handling post-binding data such as sorting and have used this for years without describing behavior. Perhaps this is the event you are looking for?



+1


source







All Articles