Get the Data of cell thats Dirty on RowUpdating Event - c#

I am using UltraWebGrid from Infragistics for grid functionality in my ASP.NET 4.0 Application. I have enabled inplace editing for few columns in the grid. I wrote the logic for this event as
protected void UltraWebGrid_RowUpdating(object sender, RowUpdatingEventArgs e)
in code behind. Along with this I want to know which cell in the row triggered this event, meaning Updating which field in the row triggered this event. I found we have a Row property for RowUpdatingEventArgs Class but that gets the entire row. I want to know what cell, its column name etc.. in the row was dirty that made this event happen.
Any useful pointers on this one?

You would need to track this on your own and can see when a cell is updated by handling the client side AfterCellUpdate client side event. In this event you will know what cell triggered the change and will need to store that information in a hidden field or possible a hidden column of the grid so that you can access it later
You can see all of the client side events for the UltraWebGrid in the online documentation:
http://help.infragistics.com/doc/ASPNET/2011.1/CLR4.0/?page=WebGrid_Client_Side_Events_CSOM.html

Related

Capturing data before it's changed in a datagridview?

My application has a databound gridview control. When someone changes anything in a cell, the backing datasource is changed. When the user saves the changes, I'm basically just getting using:
var changes = ((DataTable)this.bindingSource1.DataSource).GetChanges(DataRowState.Modified);
With the changes collection, I can then update the appropriate database tables.
Now, however, the user wants there to be a log to contain before AND after data. I could just make a copy of the datasource and hold onto it and compare what was changed, then write out a log with the before and after. But, isn't there some other way that won't require me to keep a copy of the entire datasource? I'd like to only keep a before image of data that was actually changed, instead of the whole datasource pre-changes.
I know there's an event that I could use that fires when a cell is changed. Is there a way to grab the original pre-changed data by using this event? I can't think of how, though, since the event only fires if the cell is changed.
Saving the entire datasource seems impractical, so I'm hoping there's another way to do this.
dgv.CurrentCellDirtyStateChanged += new EventHandler(dgv_CurrentCellDirtyStateChanged);
void dgv_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
//Work here, this is called before the cell change has been comitted
}
See here for more info on this event

DataGridView first column,first row, is selected on Load, I don't want this

So basically the very first column in the first row is always selected, I can't figure out a way to have it so the gridview has no selected cells. Any help?
I was having quite a bit of trouble with this myself. I have a user control with a DataGridView that is populated on application/form load. The selection of the first row seems to happen after databinding is complete and the grid is populated/rendered. The user control load event (and presumably, form load as well) fires prior to that - so calling gridView.ClearSelection() or nullifying gridView.CurrentCell in those load events has no net effect.
What finally worked for me was calling .ClearSelection() from the DataBindingComplete event of the DataGridView itself. This worked like a charm.
I had this same issue and nothing was working. The solution that worked for me was setting the 'Tabstop' property to False and calling the ClearSelection() method immediately after the data bind.
Set the DGV's CurrentCell property to null after data binding the DGV:
dataGridView1.CurrentCell = null;
Note that doing this won't prevent DGV events associated with row and cell selection from firing; you'll have to add selected row or cell count checks on RowEnter events, something like this:
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.SelectedRows.Count == 1) {
// Do stuff since a row is actually selected ...
}
}
after bounding data just call
dataGridView.ClearSelection();
I think you tried to call it before setting data to dataGrindView, if you even ever tried it
You should call: ClearSelection after event: DataBindingComplete
I had the same issue in my case, instead of set the first row visibility to false. It would be better to set the GridColor value to avoid risk on SelectionChanged Event.
Put dgv.ClearSelection() on DataBindingComplete Event and set GridColor to your DataGridView BackColor.
Set GridColor to visible color (e.g. : Gray) on your populate method / firing event.
IF I understand the Q. This prevents a cell from showing selected after data binding.
So the back color stays white. You can also Edit the columns and set it there.
DataGridView.DefaultCellStyle.SelectionBackColor = DataGridView.DefaultCellStyle.BackColor;
I also wanted read-only DataGridView, and in my case, a separate thread is slowly obtaining data and handing it to the GUI thread via a multi-thread list, and Form timer. In this approach, the GUI thread expands the data grid as needed while allowing browse.
With suggestions above the selection could be hidden, but none could prevent the cell from getting reset when my GUI thread calls dataGridView.Rows.Add() with a selection. This includes hooking events to prevent the selection, and disabling edit mode.
I found the behavior I wanted with
dataGridView.AllowUserToAddRows = false;
Now I have a dynamically sized, asynchronously loaded data grid that is read-only.
I did not like the BackgroundWorker solution, because progress is quite a burden on my loading code. Nor did I like the requirement to rebuild a new DataTable every refresh of the grid. I could not find any hints on refreshing the DataGridView with one DataTable that is being built up, but it seems like this should be possible.
Make sure your are not calling the method to load the data from the form constructor. If you call it from the form.load()
also after the datagridview is loaded do this
DataGridView.Rows[0].Selected = false;
Most of the time, it is caused by a small mistake, maybe the datagridview is set on a group box. If there are more group boxes then the selection will stop on the first group box, so keep the group box by priority basis.
I had the same problem and have solved it by overriding the OnPropertyChanged event of the GridView
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
this.ClearSelection();
}
Event _MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
GridView gv = (sender as GridView).GetDetailView(e.RowHandle, e.RelationIndex) as
GridView;
gv.ClearSelection();

Best way to fetch gridview row on row databound event?

Hi I was just wondering what is the best way to fetch gridview data using row databound event of gridview. I am previously used to Eval but read its not recommended as it uses reflection.
What do you mean "Fetch" the data, by the time the the RowDataBound event has triggered, there already has to be a row of data, that is why the event has executed. If you want to access and mapipulate the data then it is in e.Row.DataItem.
Edit
To answer your comment, using Eval in the markup and putting code in the RowDataBound event handler and accessing e.Row.DataItem tend to be used under different cirumstances. If all you want to do is take the data and bind it to the property of a control then using Eval() (or Bind() for that matter) is fine. However if you want to do something more complex then you might need to do it in the RowDataBound event handler. For exam-ple you might have a grid of customer accounts and ballances. For customers where thir ballances are overdue you might want to turn the row red to highlight the fact that thier accounts are overdue. You could not do that using Eval or Bind in the markup so you would check e.Row.DataItem in the RowDataBound event handler and then decide whether to change the colour of the row.

Commit current dirty cell on clicking save button from any datagridview on form

I have a form with multiple datagridviews. On save the entire dataset will be serialized to a strongly typed property bound to a sql varbinary(max) Works fine.
Of course the current "dirty" cell will not be saved as mentioned here :
DataGridView -Value does not gets saved if selection is not lost from a cell
My problem is the user could be coming from any one of 20 datagridviews when they click SAVE.
Is there any way, short of checking for a dirty cell in each datagridview to commit any dirty cell before the save ( clicking another textbox control before the save does the trick but calling the focus() of that textbox prior to the save does not )
I was thinking perhaps catching the event of leaving the grid but it seems the base problem is clicking a button (for reasons I think I understand) does not fire lostfocus events for the current control and it doesn't seem the click handler args knows what the last current selected control is.
Guidance appreciated.
TIA
Hook up all your datagridviews to the same Leave event handler. The leave event is processed before the click event. Inside your leave event, capture the datagridview that just lost focus. When the save button is clicked, check if the last datagrid view that was left has any unsaved data...
Example:
DataGridView _lastDataGridView = null;
private void dataGridView_Leave(object sender, EventArgs e)
{
_lastDataGridView = sender as DataGridView;
}
private void saveButton_Click(object sender, EventArgs e)
{
if (_lastDataGridView != null)
{
// check if data needs saving...
}
}
Edit 1:
As for you not receiving the leave event before the click event, I'm not seeing that behavior. To reproduce what I've done, create a form with a DataGridView and a button. Add two text box columns to the DataGridView, hook up the events as described and see if the _lastDataGridView member is set when executing sendButton_Click. It is on my end.
Edit 2:
After trying my datagridview I noticed that the data was always being saved. So I suspected you had some different settings. I turned on "VirtualMode." This causes the same behavior that you're describing. If possible, try turning VirtualMode off and see if the data gets saved to the DataGridView as expected. Otherwise try implementing the suggestions outlined in this MSDN article.
Why not use the DataGridView's CellEndEdit event to mark a boolean flag that the DGV is dirty? Assuming you are using SQL, you would create an UpdateCommand, SelectCommand, and DeleteCommand for each DGV. When you want to "save" the changes, just call DataAdapter.Update(myDataSet, "TABLE NAME"); for the DataAdapter associated with your DataGridView. I use this technique now for one DGV and it works fine.

edit datagrid row

i want to know how to edit a single row (which i select) from a data grid
for example i have a datagrid with columns A, B and C
and i have a couple rows of data, approx 10 rows.
lets say i want to change the value of data within row 4.
how would i do this?
i am using visual studio 2003, but i guess if visual studio 2005 would be okay too. for the coding I'm using c#
thanks..
All grid-like components of asp.net have the same machanism as it comes to starting to edit a single row. Actually it's default for asp.net only to edit a single row in a grid.
Needed to start editing is to include asp:button or asp:linkbutton in the ItemTemplate with the CommandName set to "Edit". This one of reserved commandnames all grid-like components knows how to respond to. Clicking this button in DataGrid will raise the EditCommand Event. In this event you have to set the EditItemIndex of the grid equal to Item.Itemindex of the eventargs. This will render the row vaccordeing to the EditItemTemplate.
In this template you put 2 buttons or linkbuttons. One should have the CommandName set to "Update" and one should have the CommandName set to "Cancel".
The "Update" button raises the UpdateCommand event. In which you execute code that store the data in the row to its storage (eg.: database) and sets the EditItemIndex to -1 --> all rows are rendered read-only(ItemTemplate or AlternateItemTemplate).
The "Cancel" button raises the CancelCommand event. In the event handler you have to do si set the EditItemIndex to -1.
This description is only true for DataGrid en not for the in asp.net introduced GridView which handles most of this "Boilerplate"code it self working together with the datasource controls. Google the web for more info on this. it's to much to explain here right now.
Hope it helps?
Take a look at the documentation for adding an EditItemTemplate to your datagrid. You use the ItemTemplate for view-only, display elements and you use the EditItemTemplate for controls used to bind against a single row that you select.
Here's a link that might help:
http://www.gridviewguy.com/
Is your data in a DataTable before making it a DataGrid, or can you put it in a DataTable? You can update/delete/edit rows in a DataTable, here's a link with code snippets, pretty straight forward:
http://msdn.microsoft.com/en-us/library/tat996zc(VS.80).aspx

Categories

Resources