Gridview updating database
Gridview updating database - best camera for young adults
In this article, I am going to take a simple database table that has following fields: Auto ID - Int, Auto Increment, Primary Key Page Name - Varchar(50) Page Description - Varchar(500) Active - Bit(1) In this article, I am not going to explain the workings of Sql objects but focusing on the events and methods of the Grid View that will help us in updating and deleting the records.Now let’s start with putting the Grid View on page. Data Key Names="Auto ID" Auto Generate Edit Button="True" On Row Editing="Edit Record" On Row Canceling Edit="Cancel Record" On Row Updating="Update Record" Cell Padding="4" Header Style-Horizontal Align="left" On Row Deleting="Delete Record" Row Style-Vertical Align="Top" Fore Color="#333333" Grid Lines="None" In the above code, I have kept Auto Generate Columns as false so that Grid View will not render all the fields of the data source automatically.
I love learning new technologies, programming, blogging and participating the forum discussions more...
So I have to specify how I want them to appear in the normal view and edit view.
That’s why I have placed different templates like Item Template (for normal view) and Edit Item Template (for edit view).
Till now our Grid View is ready to be populated, now let’s write a method to populate the Grid View.
To populate the Grid View, I am calling Bind Data() method from Page_Load event after checking Is Post Back property of the page, so the Grid View will only be populated when there is no postback on the page (when the page loads for the first time).
To get the Primary key, I have used Data Keys property of the Grid View with the index of the row, being edited and stored into a integer variable after parsing.
Now I have declared a Grid View Row variable named row and stored the current row being edited into it.I will be using Sql objects directly into methods to keep the example simple and straight forward.In practical scenario you should use your existing architecture to populate and update the data.In this method, I am specifying the Edit Index property of the Grid View to -1.As there is no row at -1 position so Grid View will be shown in normal mode.After that again, I am doing the same thing that I had done in Cancel Record method, I mean setting Edit Index property of the Grid View to -1 and rebinding the data again by calling Bind Data() method.