Assign Current GridView Data Table to Session? - asp.net

I have a GridView consisting of editable text boxes. When the user enters a value, the OnTextChanged function calls my UpdateColumns() method, which performs various calculations and alters the whole GridView. I have limited user input to only numbers so that the operation errors out when the user enters alpha characters in any cell. Upon this error, I would like to revert to the Data Table that existed immediately prior to the user entering the alpha character.
Is there a way to capture the current GridView's data source and store in a DataTable?
I have tried the following (after the data table has been bound):
Dim dt As DataTable = DirectCast(gvBuildingBlocks.DataSource, DataTable)
Session("buildingBlocks") = dt
'If textboxes are all numeric, perform calculations here
'Else display error here and bind table below
gvBuildingBlocks.DataSource = CType(Session("buildingBlocks"), DataTable)
gvBuildingBlocks.DataBind()
The above results in an empty gridview, as the value of gvBuildingBlocks.DataSource is empty. Is there anything else I can do to get the values?
I imagine I could loop through all the rows and save them somehow as follows:
For Each row As GridViewRow in gvBuildingBlocks.Rows
'Save cell value here
Next
But I don't know what code to use to save the values?
Any help is much appreciated.
Thanks!

I understand you are doing calculations on rows and you want to rollback to original values if the user input is invalid. You could accomplish this at the row level by calling DataRow.BeginEdit() before the edit, and DataRow.CancelEdit() if the input is invalid. See this example
Don't forget to call DataRow.EndEdit() if the edit is valid to commit changes.

Related

ASPX FormView data not displayed with valid data from datasource

I have an aspx Formview set to 'Edit' mode and a valid SQL datasource that returns the correct data row based on the row ID. These lines in my grid rowcommand event:
DailyDataSource.SelectParameters["ID"].DefaultValue = recid.ToString();
FormView1.DataBind();
DataRowView drv = (DataRowView)FormView1.DataItem;
returns a valid DataRowView object with the correct data record. However, the DataBind() appears to not be working - no data appears in the entry text boxes. Unless I set the entry textboxes to 'ReadOnly'. They then display the returned data, but I (obviously) cannot edit it.
I can edit and enter data in the non-readonly text boxes, and the Save command saves the data, and it displays in the gridview, but if I try to edit it, I again get a valid set of data, but no displayed data.
My question: Why is making these fields readonly allowing the data to be displayed?
Second question: Why is the data in the datasource returned record not appearing in the editable textboxes?
BLUF: I needed to do the formview data load in the PostBack event, in order to get the correct session variables which were set by other events. Question of "How can I be so dumb when I am so smart?"

Maintaining Row Selection in ASP.NET GridView Control

The Setup:
I currently have a page with a GridView control on it inside of an update panel, using a SqlDataSource. I have a timer setup to update the GridView every X amount of seconds. Typically for what I am testing every time the GridView updates about 4-5 new rows of data are added to the gridview, while the last 4-5 get tossed out. I am only displaying 15 results at a time and will have new results coming in every update.
The Problem:
I allow the user to select the rows, while the GridView is being updated. I am handling this by setting the SelectedIndex property. However, when I select a row and then the grid is updated the row the user selected gets pushed down about 4-5 rows and the data in the previous selected index is selected instead. So where they clicked is selected at this point, not what they clicked.
I need a way to determine, if possible from the SqlDataSource/Gridview, how many new rows have been added to the gridview. OR a way to maintain the selected data by the data in the row and not just the SelectedIndex.
Any help is appreciated, thanks.
RESOLVED:
Ok I went ahead and added a new invisible column to my grid, and am now keep track of the unique ID's selected from the DB. By setting an array before databinding, and comparing that to the new array I get after databinding I was able to use a simple Intersect to determine the number of rows that are the same. Then I used that to determine from the total how many are new this postback.
Just an idea:
I think you can use an invisible column (more specifically an ID column) to store the selected rows' IDs value in the Session object and then after the grid updates, you can retrieve this value(s) and select the row(s) again if they are still present.
If you have custom GridView OnRowUpdating event.
public void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Session["CurrIndex"] = GridView.SelectedIndex;//index before insertion
Session["RowCount"] = GridView.Rows.Count;//row count before insertion
//Add new Rows
GridView.SelectedIndex = (Int32)(Session["CurrIndex"]) + ( GridView.Rows.Count - (Int32)(Session["RowCount"]);//update selected index
Session["CurrIndex"] = GridView.SelectedIndex;//restore the index into session
}

GridView Initial Editing mode?

I am writing a web application that is used for tracking and entering some everyday user data... Every day I need to enter some data for multiple users manually...
I will choose the date with the calendar, and for that date I will get DB values assigned for all users on chosen date...If there are some entries in database for that date i will display it in gridview...
Here is the look of gridview for chosen date...
USERS DATA
------------
User1 Data1
User2 Data2
User3 Data3
But, if I choose the date for which there are no entries in DB, I would like to display the gridview containg User names in each row, but I want the second column (DATA field) to be initially editable for ALL rows (the whole second column should contain textboxes for inserting values, not only selected row as in standard editable gridview mode) allowing inserting of wanted values...
I want it to look something like this
USERS DATA
----------------
User1 TextBox
User2 TextBox
User3 TextBox
My idea is to get Users list from DB and create a datatable with first column containg returned user names, and second column empty... This datatable will be datasource for gridview in which I am going to disable editing mode, and in normal (displaying) mode I am going to have TemplateField containg of textboxes bound to that datatable in second column, and labels displaying usernames in first column... When entering all values there will be SAVE button who is calling a method that will read entered values from datatable and save it to database...
Is there a better way to implement this functionality? If Templatefield textboxes are bound to datatable (as gridview datasource) textboxes entered values are going to be accessible from datable object, but Save button click causes postback, and I don't know how to preserve datatable state? Is there a better way to get wanted gridview with whole second column availibe for inserting new values?
Thank you in advance for your help & time!
Try this
http://www.highoncoding.com/Articles/219_GridView_All_Rows_in_Edit_Mode.aspx
http://csharpdotnetfreak.blogspot.com/2009/05/edit-multiple-records-gridview-checkbox.html
I would retrieve the full dataset using a left join in the database, and if there is no entry for a user, that user will be returned with a blank value. Something like this:
select users.user,
isnull(entries.entry,'')
from users
left join entries on users.userid = entries.userid
I think you can override the OnDataBinding (or OnDataBound) method to check and see if the second column is empty, and place the field in edit mode if it is. This way, if some users have entries but some don't, only the users with no entry will be in edit mode. Any user with an entry will be in display mode. There is a sample for this at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx.

Gridview Edit not working after Sorting ASP.NET

I am using C#,ASP.NET
I have a Gridview for which I have provided Sorting, Edit functionality. I am not able to perform EDIT when I perform Sorting. After sorting edit is set on some other row. I think there is some problem with the index it is taking..
Can any one help me how to fix this..
Regards
sbmarya
I think the issue is that the sorting is using a different call/datasouce than the editing. So in the RowEditing event I am getting an index relative to the sort order (either ASC() or DESC()). But then I am binding using getUsers() which is returning the data in a different order.
What I did is I stored some kind of a flag(Value) in ViewState to indicate what sort order I am in and made use of that when I am binding in the Editing event, so that I can call the right method to return the same datasource.
Regards,
sbmarya
I faced this problem as well. This is how I fixed it. (In my example the gridview is sorted on a column called submit date).
a. When the underlying dataTable of the gridview is crated and sorted store it in a session variable. The trick is, before storing to a session variable make sure you store the sorted view.
dt.DefaultView.Sort = "Submit Date" + " " + "DESC";
GridView1.DataSource = dt;
GridView1.DataBind();
Session["gridViewData"] = dt.DefaultView.ToTable(); //Only storing dt will not have the sorted table stored in session.
b. next, perform all edit/update operation using the dataTable stored in session in the above step. It will always come up in proper sorted order and you will not see the issue of row index changing unexpectedly after update.

Get Changed Rows of GridView ASP.Net

How Can I find all the rows that has been changed in gridview. I can not use Ajax in any form
First get the contents of your grid before it was changed (such as caching the results of the original gridview datasource binding). Then go through the dataset/datatable/however you want to store it, and compare the contents with the current rows of the gridview.
There's no real efficient way to do this, no method like GridView.GetAllChangedRows(). So, what you might do instead is keep a behind the scenes List that you add to each time a row is modified (use the RowUpdated method), then clear this list when needed.
It depends upon how many columns you want to edit in a row.
If you have only one editable column in a row then you can associate a javascript method with that control which you want to modify and in that method you can get a rowid which you can save in another hidden field and in server side you can get all rows whose ids are stored in hidden field.
If you have whole row editable in that case the best approach I think you should save the original data source somewhere and also set a javascript method with rowclick event to get rowid which user selects. Then when user clicks on submit button get all rows whose row ids are stored in hidden field then compare those with same rowid in datasource. This is the best approach from my point of you.
Let me give you an example, suppose there are 1000 rows in a grid and user clicks on only 180 rows. In that case we will compare only 180 rows and wont compare rest of the rows.
Please let me know if somebody has better idea then this.

Resources