Simulate a DataGridView.DataChanged event? - c#

I want to make a single event that will capture every change in the data in a DataGridView. The list of events is pretty long. I was thinking I could bind the DataGridView to a DataTable since it has less events and it gets notified whenever the user changes the data in the DataGridView. Since the amount of data is tiny (2 columns and about 5 rows) but the user should be able to add, remove and edit rows, it would be a lot simpler if I put all possible changes in a single event so every other class that needs it gets notified when anything changes in the DataGridView's data by handing a single event. I don't want to capture when the user is typing in a cell or when a row is resized or anything that won't be normally considered as representing a change in the data. That's also why I think using a binded DataTable is a good idea. So my question is, which set of events is the minimum I need to handle in order to capture every possible change in the data contained in a DataGridView or in a DataTable (preferably a DataTable I think)?

I would try to bind to the following DataTable events:
RowDeleted
RowChanged
Those should give you what you want - notification only in case you edit, delete or add a row.

Related

C# - Customize column sorting of DataTable-bound DataGridView by the cells' background color

I have a DataGridView component that is bound to a DataTable like so :
DgvResults.DataSource = ResultsData;
Said DataTable is created dynamically by converting a Dictionary which is itself obtained from comparing 2 excel files together and storing the comparison results (I think this portion of the code is not really relevant here but I can certainly show it if needed).
My DataGridView's Sorted event and the end of the above comparison process are bound to a method I have created which loops through all the rows and cells and assigns a certain background color to each cell based on certain criteria, again not really relevant to the question.
My question is : Knowing that bound DataGridView components cannot use the SortCompare event, is there a way for a click on a column header in the DataGridView to result in the results being sorted by background color? I assume some kind of custom sorting mechanism using a custom comparer which has access to the DataGridView as well as the index of the column clicked will be needed but I cannot seem to find how to wire all this together.
I can provide more code if needed but the bulk of the issue resides in how to provide a custom sorting behavior for a DataTable that can be triggered by sorting a column on the associated DataGridView.

Datagridview dataload when searching

I have a datagridview which displays data from database. I have added a a textbox as a searchbox and added a text changed event. In that event I have written Search query with 'LIKE' based on text in textbox. So when I type a character it will instantly search the database and display in datagridview.
But my problem is, for large amount of data, for example a million rows, this text changed event hangs the datagridview. cant display data to datagridview fastly. Any way to speed up the process?
I would use a BackgroundWorker to search my data and handle result only if the box content didn't change in the mean time. I would also add a delay on search to avoid useless search. Actually, when needed this kind of search i prefer handle keydown on Enter or/and a button to be sure that we search when we want to.

update DataGridView without losing selection

I'm trying to update data I have in a datagridview (based on checks set by the user), without losing the selection (on which a calculation is made). This works for me, but is a lot slower than clearing the complete dgv, making a new datatable and binding it.
This is what I tried :
when the datagridview need to be completely updated (different amount of rows), I clear the datagridview, make a new datatable and bind it. This works nice and fast, but the selection is lost. It's no problem the selection is lost, because the data is completely new.
sometimes I only need to update the values in the datagridview (and I don't want to lose the selection). I tried to either update the values in the bound datatable (works) or the values in the datagridview directly (this also works). However, both are a lot slower than just making a whole new datagridview using the first method :-(
when updating the datagridview, I stop drawing the panel on which it is placed
all events that the datagridview fires are suspended
autoresize etc is off
Can anyone explain why updating the cellvalues is so much slower than redoing the whole thing?
Is there another way to do it without losing the selection? I could remember the selection and set it again, but then I lose the way/order cells and columns where selected.
I finally found where the big delay occured. I was completely focused on the DataGridView, but it turned out the delay happend in the DataTable that was bound to it.
In order to do it fast :
first set each row of the DataTable to BeginEdit, so changes are not acted upon. After updating the DataGridView, do not make your DataTable accept the changes, it will make you lose the selection in the DataGridView.
[code]
for (int Row = 0; Row < MyDataTable.Rows.Count; Row++) MyDataTable.Rows[Row].BeginEdit();
// do your changes in the DataGridView (not the DataTable) here
UpdateDataGridView();
// never accept the changes in the DataTable, it will lose the selection in the DataGridView. Problem is that the DataTable is never updated, but this is not a problem in my case.
//MyDataTable.AcceptChanges();
[/code]

WPF DataGrid handlers for Add and Edit rows

I've been trying to find out how to be notified (i.e. have a handler) when user edits or adds a new row in the DataGrid. As for editing, the closest I could find is RowEditEnding handler, but the problem with that handler is that I can't access the new values of the cells in the row being edited. This handler has only the old values, cause the event is triggered right before the data is committed.
Anyways, the list that is bound to the DataGrid is being properly updated both on edit and add, but what I want is to do appropriate SQL queries and update the sql table cause the list is made out of data pulled from the Database.
Any ideas?

.NET - DataGridView - Updating to database when user adds a row

Hey guys. I'm having an issue with using a datagridview for a user entering data that's saved to a database. Basically, I just want them to throw stuff into a row and then my code will insert the data. I know there has to be some event for what I'm trying to and I just can't figure it out.
I tried the LeavingRow event, but the problem with that is that the final value hasn't updated when this event triggers. What I mean is, say we have a row with four columns, and the user has entered data into the first three columns. They enter info in the last column, then push down to enter a new row. The insert fails, because it thinks the last column is still empty.
I also tried UserAddsRow, but had that fail because it triggers as soon as the user starts typing to add a row, so there would be only a single character in the first column of the row I was trying to add.
Does anyone have suggestions for getting these, or some other event working for my purposes?
The two inelegant alternatives I'm weighing are a) stringing together some labels/textboxes and an add button or b) using the DataGridCell_ValueChanged event, by having some nested try/catch that will try to insert first, then if it fails try to update, and then do nothing if the update doesn't work. It's grossly inefficient, but I think it would get the job done since there's only going to be a couple dozen rows tops at one time in this datagridview.
Thank you for your time.
I typically use the RowValidated Event. I use this event because it allows for a whole row to be entered / edited before trying to save the changes.
When handling the event I get the rows that have been added, updated or deleted from the dataset (DataSet.GetChanges) and then perform the appropriate actions per type (Add, Update, Delete).

Categories