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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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 |
| Sponsored Links |
| |||
| 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 |
| |||
| 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") + "')");
}
} 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 :)
}
} 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);
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |