View Single Post
  #2 (permalink)  
Old 09-26-2007, 10:53 PM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Smile Re: Grid View Control in asp.net

Hi,
the GridView's TemplateField, containing an ItemTemplate, which in turn contains a Button control. Such a button, with the CommandName set to
Code:
Delete, will cause the GridView to delete the associated row when pressed.  Since our aim is to show a confirmation message before the deletion of the row, we will also make use of the button's OnClientClick property.

The button control's properties are assigned as follows:

·         OnClientClick to JavaScript confirm message

·         CommandNameto Delete

The DataKeyNames property of the GridView control is used to specify the field or fields that represent the primary key of the data source.

Listing 1 – Display Data Using GridView Control

Code:
<asp:GridView ID="GridView1"runat="server" 
DataSourceID="SqlDataSource1" 
AutoGenerateColumns="False" 
DataKeyNames="EmployeeId">
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
   <asp:Button ID="btnDelete" Text="Delete" runat="server"
OnClientClick="return confirm('Are you sureyou want to delete this record?');"
CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="EmployeeId" DataField="EmployeeId" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
</Columns>
</asp:GridView>
Next, we set the SqlDataSource DeleteCommand and its parameters. EmployeeId is the primary key in the Employees table. This key is used to delete the row. The GridView needs to find which row has to be deleted, hence we used @original_EmployeeId and not @EmployeeId. If we don't prefix this parameter with original_ then there is no way to find that row which has to be deleted. Listing 2 – The SqlDataSource
Code:
<asp:SqlDataSource ID="SqlDataSource1"runat="server" 
ConnectionString="Server=localhost;Database=Northwind;userid=username;password=secret;" 
SelectCommand="Select * from Employees" 
DeleteCommand="Delete from Employees whereEmployeeId=@original_EmployeeId"
OnDeleted="SqlDataSource1_Deleted">
<DeleteParameters>
<asp:Parameter Type="Int32"Name="EmployeeId"></asp:Parameter>
</DeleteParameters>
</asp:SqlDataSource>
<asp:Label ID="lblShow"runat="server" ForeColor="DarkRed"></asp:Label>
To show a message to the end user after the row has been deleted we will use the Deleted event of the SqlDataSource control to do this task for us and also supply and error messages. Listing 3 - The Deletion Code C# protected void SqlDataSource1_Deleted(objectsender, SqlDataSourceStatusEventArgs e) { if (e.Exception == null) { if (e.AffectedRows == 1) { lblShow.Text = "Record deletedsuccessfully."; } else { lblShow.Text = "An error occurredduring the delete operation."; } } else { lblShow.Text = "An error occurred whileattempting to delete the row."; e.ExceptionHandled = true; }
Hope u get idea about deletion
Reply With Quote