Partial view checkbox value not getting updated in MVC - c#

I have a partial view, which is bound to a collection of data having property called as IsDelete, used to check if the user wants to delete that specific row.
On click of button, the data comes as expected.
The problem arises as I try sorting this collection, suppose I checked the first row check and now sort the rows, even after sorting still the first row checkbox is shown as checked while the data has moved from first to some other row after perform sorting.
Hence, when I try submitting the page, I am getting that user has selected to delete first row each time in this case, instead of the actual selected row.
Note : I am doing a server side sorting, using linq and then updating the partial view

Related

GridView Sort and delete; Wrong row

So I have a grid view which gets data from a database. I want to be able to sort and page this table, as well as to remove single rows.
Now the Problem: I need to bind my data every time (also on postback), else my datasource will be null when trying to sort, but by doing so I will also get the wrong row each time I try to delete (or edit) a row.
The problem was that I thought that I had to bind the rows each time. All I had to do each time is to set the DataSouce and ONLY bind if (!IsPostBack). Then onSort i just bind again and it will be sorted. Also the delete-Function will then get the right row.

Gridview Automatically Selects first Row after a sort

I am working on a master/detail gridview and detailsview in asp.net web forms using an objectdatasource. The details view is displaying extra information about the selected row from the gridview. When I sort the grid, I want the currently selected row, before sort occurs, to remain in the details view after the sort is completed. Instead, the gridview is auto selecting the new first row whenever I sort.
I found a partial solution to this problem. If I set WhateverGridview.SelectedIndex= -1 onsort and the value becomes null. This makes it deselect any rows after a sort. This leaves the details view blank. However, what I want to do is maintain the selected row not nullify it.
So, does anyone have a good way to retain the selected value or prevent the details view from displaying after sort event fires.
Here you need to use the GridView.EnablePersistedSelection property. Set this property to true.
Setting this property to true means that GridView will make sure that the selection of a row is based on data-key values.
By default GridView makes row selections based on index. This is the reason why when you sort, gridview is selecting row based on index and you lose your actual selected row.

How i can refresh wpf form or list view on click event performed on second form while performing updates on it?

I have list view on first forms if i select a row and click edit button second form open their i do updates in selected row and click ok then database get updated but i don't know how to update list view display on first form according to updated database?
I will bite the bullet and give you an answer... note that this will be all I offer, no long discussion!
Your ListView will be bound to an IEnumerable of data objects. You will be passing the selected data object to the second form, so any operation done on that data object in the second form will instantly show up in the first form - because you passed an object it is by reference, both forms are looking at the same thing.
As for updating after the database save - you can save yourself some trouble and only get a fresh copy of the data object if there was a problem with the save (so what is currently in the database doesn't match the contents of the data object), or if there is more than one concurrent user of the database. If this is the case then you know the identifier of the data, just fetch a new copy then replace the data row in the IEnumerable that the ListView is bound to.
Remember: data binding is your friend. And it is easy to insert and remove from a list.

DataGrid problems. Using DataSet.Merge with "Edit Template"

I am trying to debug an issue with a WinForm application.
The application uses a DataGridView bound to a DataSet.
The user can select a row and click "Edit" and an Edit Form appears so the user can edit the row and save his changes.
If the DataGridView is sorted by one or more columns and the user edits a row more than once and changes that rows position in the DataGrid (by changing one of the sorted columns TWICE), the DataGridView gets out of sync and I get Exception errors.
Here is how the code is written:
When the user clicks a row and hits edit (the user may only select one row), that row is passed to the EditFom. The edit Form creates an empty DataSet and loads that Row. When the user is done, the calling form uses DataSet.Merge to merge those changes back into its DataSet.
Here are a couple of scenarios that tell me it is womething to do with the "merge" and sorting:
(Note, I never change the actual "sorting", I am talking about changing the value of one of the sorted by columns in one of the rows.)
If the user edits a row directly in the Grid and changes the value of one of the columns being used to sort, then changes it again, etc. The row moves around the Grid, like it should and when the user clicks save, everything saves fine.
If the user edits a row (directly in the Grid OR via the "template") and doesnt change any of the "sort" columns, just makes an edit (therby setting the "State" of the row to Modified, right...), THEN the user edits the row using the Template (which goes through the seperate DataSet and merges the data back when done), and changes one of the columns being sorted (when done, the grid should resort and the row should "move"), the row doesn't get REPAINTED, it stays in the same position in the grid that it was in before the template edit. (Although, the data saves fine and you can refresh the grid and it sorts properly.) Evidently, the DataGrid isn't being notified that the RowChanged in this scenario (or it is, but it isn't doing anything about it).
If the user edits a row directly in the Grid, change a sort column (the row moves), then edits the row using the template and changes that column again, we get the same results as 2, above. The data saves fine, but when the user leaves the edit template, the grid doesn't resort.
(This is the one that causes the Exception error.)
If the user edits a row using the template and changes a sort column and leaves the template, the datagrid resorts and the row moves properly. Then, if the user edits the SAME row using the Template Edit and changes the sort column AGAIN. When the user leaves the edit template, the datagrid doesn't resort and when the user saves the data and the grid refreshes, we get an error:
System.InvalidOperationException: DataTable internal index is corrupted: '5'.
And sometimes, we get an error complaining the primary key is missing.
Any advice on how to "debug" this? I feel like it has something to do with the "internediate" dataset the edit template uses and the "Merge". It is like the DataGrid datasource is getting out of sync with the DataGrid Rows.
I should have done some Google'ing before posting this. It seems to be a common error and I found a HUGE post about this on the MS site. Unfortunately, I haev tried most of their suggestions, but most likely I will find my answer there. I just wanted to make a note of this, in case anyone is interested:
http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/18544cd3-1083-45fe-b9e7-bb34482b68dd

SelectedIndexChanged fires a detailview grid but how to catch it's info

This is how i have it setup:
DAL: a dbml file for the context
BLL: All my BL rules
Types: I made type for every object that i return to my presentation layer.
Now that i made types and use this structure i find it hard to understand how to manipulate data. Normally i could just bind my datacontext to a grid and just activate all the datagrid's possibilities without having to worry about anything.
Now for example when i want to enable sorting, i have to write my own logic to return teh right rows to the grid etc.
I have a multiview with two datagrids. The first datagrid is an overview grid and has an option to select an entry. When i select the entry i switch to my second view and I want to show the details for the selected object there.
However... I bound a List to the first grid and now I don't know how to find out what entry is selected. I have an event handler for the SelectedIndexChanged.
I want to catch the right details for the 2nd datagrid there.
assuming that your objects from the BLL have an Id property, apply a the name of the property as a datakey to the first GridView. In the aspx page, add the following property to the gridview
DataKeyNames="Id"
On the SelectedIndexChanged, get the Id of your selected object using the following code:
string id = myGrid.SelectedDataKey.Value;
Now that you have your Id, you can query your needed object from the DB.

Categories