Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts

Saturday, December 26, 2020

GridView buttons and commands Delete

 You have a GridView and you want a simple Delete button. What are your options?

Most basic would be to use the CommandField (the Wizard will take you here, too).


Main points are:

In the <asp:Gridview> , after the <columns>, before the <asp:BoundField> items, you would see:

<asp:CommandField ..... (tbd???)


and it is incumbant upon the asp:SqlDataSource to have a section called:

DeleteCommand="" with the actual SQL to be used.


The only other power you have is to include a OnRowDeleted="{create event}" in the <asp:GridView> block, to do something like handle EXCEPTIONS (example: gvResources_RowDeleted).

You can customize how the "Delete" button looks like (Wizard will default to a link that says "Delete" in the same column as Add and Edit, generally in first column) by a bunch of optional properties in the <asp:CommandField> tag:

ButtonType="Image"

DeleteImageUrl="~/Images/deleteIcon.png"

DeleteText="Delete" (default from Wizard)

??? ShowDeleteButton="True" ????


For example, see my Forecast Resources Index page gridview.


The next way to create a "delete button" would be with a ButtonField (or a TemplateField???) but that would be overkill so long as any delete operation can be accomplished with either a SQL statement or even Stored Proc (since the CommandField can also call a stored proc).

However, let's say you prefer to, instead of calling a SQL statement stored in either the ASPX file or SQL Server (for sprocs), but in a data persistence class, maybe a ButtonField is for you.

Seemingly confusingly, to use a ButtonField, you have to specify a handler in the <asp:Gridview block named OnRowCommand="", which sounds like you are using the CommandField!!! 


onrowcommand="CustomersGridView_RowCommand"



<asp:buttonfield buttontype="Button" commandname="DeleteThis" text="Del"/>



.void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)

{
if(e.CommandName=="DeleteThis") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument); // Get the last name of the selected author from the appropriate // cell in the GridView control. GridViewRow selectedRow = GridView1.Rows[index]; TableCell contactName = selectedRow.Cells[1]; ...


    //do delete


GridView1.DataBind();
etc


I learned that if you use the CommandName="Delete" you will get an error b/c of clash with other functionality.....Also remember to rebind the grid.



Monday, September 26, 2011

1.x DataGrid, 2.0 GridView, Declarative Syntax in HTML portion...

"In Microsoft ASP.NET 1.x, one of the most commonly used ASP.NET Web controls is the DataGrid. The DataGrid control makes displaying data on an ASP.NET page a breeze: simply drag and drop the DataGrid onto your ASP.NET page, specify the columns and formatting, and write just a few lines of source code to retrieve and bind the data to the DataGrid. With a bit more effort and code, you could enable the end user to page, sort, and even edit the data within the DataGrid.


While the DataGrid was a huge improvement over pre-ASP.NET techniques for displaying data, it still suffered some limitations. For starters, binding any data to a DataGrid required some code writing, and often repetitive code at that. Furthermore, utilizing any of the DataGrid's more advanced features—handling deletes, allowing for editing of the underlying data, providing pagination, or sorting support—required additional code and time. Granted, the code for many of these additions was not terribly complex or lengthy, but any time code is written the possibility of new bugs, typos, or errors is introduced.

ASP.NET 2.0 fixes the problems of the DataGrid through a set of data source controls designed to encapsulate data that can be bound to data Web controls, and a replacement for the DataGrid—the GridView control. To access data in ASP.NET 1.x, developers had to write code; with the data source controls, however, data can be accessed through declarative syntax in the HTML portion of the ASP.NET Web page. Like other ASP.NET controls, the data source controls can be added to a page by simply dragging and dropping them from the Microsoft Visual Studio Toolbox onto the ASP.NET page Design view. Additionally, the specific data to retrieve can be specified entirely through the Design view. Once a data source control has been configured, it can be bound to a data Web control, such as the GridView, by setting the data Web control's DataSourceID property to the ID of the data source control. That's all there is to it—no code necessary!"

http://msdn.microsoft.com/en-us/library/aa479340.aspx