IT Community - Software Programming, Web Development and Technical Support

Grid View Control in asp.net

This is a discussion on Grid View Control in asp.net within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi all, I am using Grid View in asp.net using C#? I need to delete a particular row in ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-25-2007, 08:53 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Smile Grid View Control in asp.net

Hi all,
I am using Grid View in asp.net using C#? I need to delete a particular row in
grid view in run time. Can any one Help me
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-26-2007, 11:03 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: Grid View Control in asp.net

Hi buddy,

Here is the sample code for your query, check it out.

see the HTML part of the GridView code:

Code:
<asp:GridView DataKeyNames="CategoryID" ID="GridView1" 
       runat="server" AutoGenerateColumns="False" 
       OnRowCommand="GridView1_RowCommand" 
       OnRowDataBound="GridView1_RowDataBound" 
       OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting">
  <Columns>
   <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
   <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
   <asp:TemplateField HeaderText="Select">
     <ItemTemplate>
       <asp:LinkButton ID="LinkButton1" 
         CommandArgument='<%# Eval("CategoryID") %>' 
         CommandName="Delete" runat="server">
         Delete</asp:LinkButton>
     </ItemTemplate>
   </asp:TemplateField>
  </Columns>
</asp:GridView>

see the GridView_RowBound event
Code:
protected void GridView1_RowDataBound(object sender, 
                         GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1"); 
    l.Attributes.Add("onclick", "javascript:return " +
    "confirm('Are you sure you want to delete this record " +
    DataBinder.Eval(e.Row.DataItem, "CategoryID") + "')"); 
  }
}
This is pretty simple. All you need to do is to get the value from the CommandArgument property which you have already set to the CategoryID.

Code:
protected void GridView1_RowCommand(object sender, 
                         GridViewCommandEventArgs e)
{
  if (e.CommandName == "Delete")
  {
    // get the categoryID of the clicked row
    int categoryID = Convert.ToInt32(e.CommandArgument);
    // Delete the record 
    DeleteRecordByID(categoryID);
    // Implement this on your own :) 
  }
}
see how we can catch the primary key of the clicked row in the Row_Deleting event.

Code:
<asp:GridView DataKeyNames="CategoryID" ID="GridView1" 
     runat="server" AutoGenerateColumns="False" 
     OnRowCommand="GridView1_RowCommand" 
     OnRowDataBound="GridView1_RowDataBound"
     OnRowDeleted="GridView1_RowDeleted" 
     OnRowDeleting="GridView1_RowDeleting">

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  int categoryID = (int) GridView1.DataKeys[e.RowIndex].Value;
  DeleteRecordByID(categoryID); 
}
that's it...
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-27-2007, 03:18 AM
accer accer is offline
D-Web Sr.Programmer
 
Join Date: Sep 2007
Posts: 175
accer is on a distinguished road
Default Re: Grid View Control in asp.net

When you explain it like that with the visual, it sems so simple. Makes me wonder why I was wracking my brain so hard. Hurns out I was just missing a command string.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Paging in Grid Kirubhananth ASP and ASP.NET Programming 8 11-28-2008 01:51 AM
Showing the Image file thumbnail view in ListView control using VC++ 6.0 prabhat.singh C and C++ Programming 0 01-17-2008 10:53 PM
use of Custom Control and User Control? a.deeban ASP and ASP.NET Programming 1 08-20-2007 08:25 AM
How to edit grid view control in asp.net using c#? bluesky ASP and ASP.NET Programming 1 07-25-2007 05:54 AM
Data grid in PHP jegan PHP Programming 2 07-20-2007 07:38 AM


All times are GMT -7. The time now is 02:39 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0