GridView - show last added record as first displayed - asp.net

Just like in the title. I got GridView control that displaying some data from database. All i want to do is to make GridView displaying rows descending, i mean from the last added to first added. So first row i see from the GridView is displaying the last item added to database instead of added first like is in default state. I hope you understand what i mean ;).
Im pretty sure there is a property for that but i cant find it.

Don't know about any property for reversing the gridview itself, but you can always reverse the datasource ;) If you're displaying a data from db, include that in your sql/linq query.

Related

asp.net change cell value on gridview with code behind

I have an asp.net gridview with several rows/pages.
There are some fields below it which display full details of the row which has been clicked on gridview.
I then change a field value on the details below area and the new data is saved programmatically to database.
However the value on the gridview just changed below, doesn't change.
I would like to just change de cell value on the gridview programmatically so there is no need to reload the data grid.
I am basically looking for something like:
GridView1Row.row(currentrow).column(5)="xxxx"
I've search around and found some solutions based on _onrowdatabound but this is not correct, as I just want to change one cell of the grid.
Any ideas ?
The easiest way would be to change the value in your datasource and then binding the datasource to the grid again. (don't reload it from the database just change the value in the datasource)

How to programmatically change the gridview row in EDIT mode not based on the row number

I an looking for a way to change asp.net gridview row in EDIT mode based on primary key of the data and not based on the row number clicked on the gridview.
What are the options.
I'd probably have a hidden column in the GridView that contains the PK. When the edit button is clicked, you can use the appropriate handler to get the PK from that row and manipulate it as you see fit.
Update: After reading your comment, it's not that you can't get the PK, it's that the data has been refreshed between when the button is clicked and when the gridview is re-rendered as editable. In your scenario, a user could click the third row to edit and suddenly it becomes the seventh row because other users have inserted four rows.
The simple answer is don't do that! For one, it'll make your life a lot harder and 2) it'll be confusing for a user if they clicked the third row to edit and suddenly it's the seventh row on their screen.
If for whatever reason you MUST refresh the data, do it after the user commits their changes. If you need to refresh the data to make sure that some other user didn't edit the values before the user updates them, well, you're going to have to find another solution. maybe a parent/detail screen would work or add a pop-up that says that the data has been changed. (Remember that if the values suddenly change without warning that the user will be confused as well.)

ASP.net Using a Session Varriable to move a GridView SelectedDataKey to another page

Help please!
I am pulling out what little hair I have left over this and can't find an answer.
I am attempting to move the selected row in a grid view to another page and use the SelectedDataKey or the primary key value of the selected row to indicate what data should be displayed in a grid view on another page. This is the last little bit of a school project that I can't figure out.
What I am doing on the first page is to use a button click event to set a session variable that looks something like this. Session["Select"] = GridView1.SelectedDataKey; The button will then send the user to the next page. Response.Redirect("CustomerAccounts.aspx");
Once on the next page I want to use the primary key selected on the first page to show only the data with a coresponding value on the second page by using a where statement in the next gridview. So I am setting the [CustomerID]=? Session("Session["Select"])
Am I setting up the where correctly in the second gridview?
I like setting up such things with querystrings.
I made a video tutorial on youtube doing just that a while back
http://www.youtube.com/watch?v=HRjZ_0JpO2M&list=UUgtKyNvllU9E6c2Mi8OtzCQ&index=5&feature=plcp
I also made a tutorial showing how to load the detail gridview nested in the master gridview with jquery
http://www.youtube.com/watch?v=nJp14o_1wZQ&list=UUgtKyNvllU9E6c2Mi8OtzCQ&index=4&feature=plcp

How to add a FooterRow to a Gridview which has no value initially

I have a Gridview in which no rows populated initially. means i am not setting any datasource to gridview.I have to populate gridview by adding footerrow.I have given visibility of footerrow as true.So one error is coming as 'Object not set to an instance of an object'.what may be the reason for this? Can anybody help?
Actually i need to add data into the Gridview through the FooterRow.After inserting a few records,i need to insert this data into the database.So, i want this Gridview only to insert data into the database.For a particular "FileID", i have many records,thats why i am using Gridview.Is there any other method for this?
See this question: How to insert a Row in GridView.
The object reference error is probably because you have set no datasource for the GridView. In such cases, the Gridview will not render.
Edit:
I have already linked to another question which provides a very useful link to accomplish the type of functionality you desire. Since you appear not to have found it, here is the relevant link - How to easily insert row in GridView with SqlDataSource?
The article shows how you can use the EmptyDataTemplate of the GridView to enable record insertion using a GridView. Note that you will have to modify the logic a little to insert a group of records in one go, rather than one at a time.
If you have a problem with this solution, please clarify via comments.

Add New Row to GridView without DataBind

I have a GridView that allows editing the values in every column, in every row, all the time. The user enters in all their changes, clicks Save once and all changes are commited.
The user must also be able to click the New button, have a new row appear in the GridView (yep, it has to show up in the actual GridView), enter whatever data they want, click Save and have all changes go to the database.
Now. Here's the user case that's throwing me: a user arrives at the page, makes several changes on several existing rows, and then needs to add a new row, enter data in the new row, click Save, and have all changes go to the database.
However, the only ways I've seen to add a new, empty row involve rebinding the GridView, which means all of their changes will be lost. This is obviously no good.
So, my question would be: what are some approaches to adding a new, empty, editable row to a GridView without having to rebind the GridView?
The only thing I can think of would be, on the New buttons' click event, suck all the data out of the GridView (including the user's potential edits), save it to ViewState (or whatever), add the new row, repopulate the grid. This, to me, seems a little hacky, but it should allow me to turn ViewState off on the GridView.
Any ideas?
Just off my head I can think of two options. The first is to cache the original results that you are binding into the grid and when you need to add another row you add a datarow to the datatabale that you are binding to and then bind this to the grid. If there are changes in the grid then you need to update the datatable. Once all changes have been made and the user clicks the save button you can iterate through the table and update the DB with the data.
It might look like this
Page Loads
Get DB Data and put into a table
Bind the table to the grid
Store the table in a cache
When user asks for a new row
Get the cached data object.
Update any rows that have changed
Add an empty row Bind to the grid
When the user saves the grid
Get the cached object.
Make last set of updates
Loop through the row and update the DB
The other way to do this is by creating the grid dynamically but this will involve far more effort than it's worth given what you have described.
You could dynamically add the new row via javascript, and on the save command look for newly added rows. That is fairly common.

Resources