Editing a row in a gridview - asp.net

I have a two text boxes named region id and region name..and a button control
I enter some values into those text boxes and click the button to insert those values into the "gridview"and a "data table" associated with the gridview.
The gridview has the "enable editing" set to true..but when i click the "edit" button of a particular row in a gridview i get no response...i.e i do not get editable textboxes as it happens normally...
What is the solution for this?

You need to set the EditIndex of the row you are editting on the gdvMyGridView_RowEditing event:
gdvMyGridView.EditIndex = e.NewEditIndex
This will display your EditItemTemplate for the relevant row.

GridView Examples for ASP.NET 2.0: Editing the Underlying Data in a GridView
Editable GridView in ASP.NET 2.0

Edit control does not work out of blue. You have to write a backend SQL query for it to implement. The query will most likely look like this.
update regions SET name=#name where ID=#ID
where name and ID are fields of the table. Make sure the ID is the primary key, otherwise update will not work and you won't get any error.

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)

insert row to aspxgridview that is not bound to datasource

in my webpage i'm using aspxgridview that contains 3 columns with type "text column" and i don't want that gridview to show any retrived data from database so i'm not using datasource
the mission of this gridview is to accept input by user and insert the information entered by user to a table in database
i have set the properties: enableediting and enableinserting to true
the problem is when i run the project and press the hyperlink "new" in the gridview to insert a row in it i can't write any text in the text column and when i press "update" a message appeared that said: "Specified method is not supported."
please note that the property "ReadOnly" in each text column is set to false
i couldn't find example about the use of aspxgridview for insert purpose only without bind it to any datasource
thank u
Why do You need GridView for such functionality? Why not to use simple asp:TextBox with asp:Button for this?
Respond to update:
You can create List of data bound to this page. Bind this list to GridView as datasource.
Render GridView rows in edit mode and fill data to this list. But You will need to handle all gridview events connected to item editing/inserting.
List of Data You can store in ViewState.
Nowadays I would rather go with js implementation of such functionality.
As examples I can provide You with this links:
http://jsfiddle.net/rniemeyer/7RDc3/

User inserting items in a table

I have created a simple web form on ASP.NET which includes a DropDownList with the names of all the records of a table (h. PhotoAlbums) in my database. When the user selects an album, all the items of this album (photos with tootlips) appear on the page in an order. I have carried out this by the aid of ListView control assigning a LINQ source as data source.
I would like by the selection of an album to grant the user the possibility to insert new items in the selected album at the same time and on the same page besides the appearance of the already existed items in the album. I think that I should merely use a DetailsView control and adjust this, so as to insert items. Is it though possible the DetailsView to appear after the selection of an album; not when the page is loaded initially? How?
If I understand your question,
You can Initially set the visible = "false" to the DetailsView. Then in your DropDownList OnSelectedIndexChanged method you can check the selected value and set the visible= "true". In your DropDownList you should set the property AutoPostBack="true"
Hope This Helps

New row in ASP.NET GridView Control

I want to do something really simple, I just can't seem to find the EnableClientAddRow property, so I can set it to true. I have a standard GridView control on a web form. I want a button to appear on the web form. When the user clicks the button, an empty row is added to the GridView UI, so the user can enter data in the appropriate fields. The row will of course, have a "Save" button of some type in one of the columns.
I know this functionality must be in the GridView somewhere, I just can't find it. I did find some odd hacks that try to manually implement this. I'm not really interested in footer manipulations or binding tricks, just the standard add row method.
EDIT:
It appears the GridView does not support adding a row as a first-order operation. This appears to be a serious design flaw.
I typically add a new record to the underlying data source as a part of the "add record" button click action. I then re-bind the view in order to show the blank row.
The new record is typically a DataRow if the GridView is bound to a DataTable, or an object if the GridView is bound to a collection of a particular type. Not sure if that is what you consider a binding trick from your question, but it works well and is quite easy to implement.
Edit - more detail to describe the process:
Add the row to the data source, set the EditItemIndex to the newly added row in order for the row to enter edit mode, then bind the data source to the GridView. Your EditItemTemplate would contain a Cancel and a Save button. Cancel would re-bind the GridView to the underlying data source without the empty row and set EditItemIndex to -1, thereby removing the row from the GridView.
How to easily insert row in GridView with SqlDataSource
If you add a new row to the datasource, even if the row has empty values, and you databind the datasource to the Gridview, it should show up as an editable row just like any of the other rows.

ASP.NET: An editable data bound control in every record?

We need to display the result of an SQL SELECT statement on a ASP.NET 3.5 web page. Although there are a number of columns that need to be displayed, only one of the columns needs an editable text box in it... however every record in the result set needs this editable textbox.
I know I can do this manually by building an html table myself, but I was hoping that there was a way to use a data-bound control (GridView? or ListView?) to do it.
Being somewhat new to ASP.NET, I am hoping there is some one out there who has done this.
--Thanks for your help!
A little further clarification....
We need all records to be editable immediately after display - so all records either need to be displayed in edit mode simultaneously - or the regular display mode needs to have an editable text box in it.
Use a GridView and convert the column that you want to be editable to Template.
In the ItemTemplate of this field, delete the Label and add a text box.
Add an extra "Save" button outside the GridView, and iterate the GridRows, find the text box (in each row), and use it to update the database.
Add a template column and then add a text box control instead of a label control
GridView BoundFields have the ReadOnly property which can be used to prevent the field from being edited in Edit mode. Set it to True for all columns that should not be editable.
<asp:boundfield datafield="myNonEditableColumn" Readonly="true" Headertext="My Non-Editable Column"/>

Resources