Solved

Delete row from Gridview where the datasource is a datatable

Posted on 2008-10-08
5
766 Views
Last Modified: 2008-11-03
Hi,

I have a databound gridview where the datasource is a datatable. i want to enable the auto generate delete button but where do i code the actual delete command?
0
Comment
Question by:kplonk
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22670327
the way i do it normally
My gridview is configured for onrowdeleting command  - OnRowDeleting="GridView_RowDeleting"

i have a command field for delete hyperlink
<asp:CommandField DeleteText="Delete" ShowDeleteButton="True" />

the GridView_RowDeleting event contains following logic
switch ((sender as GridView).ID)
{
      case "gvDependents":
            switch (e.CommandName)
            {
                  case "Delete":
                        //find the row in the datasource
                        // delete the row
                        // accept the datatable changes
                        // rebind the grid
                        break;
            }
            break;
}
0
 

Author Comment

by:kplonk
ID: 22676309
Ok looks good so how di i find the row, i was looking at gridview.selectedindex but this is -1 is there a trick to this?
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22676522
set the DataKeyNames property

like in my case its member id filed
DataKeyNames="MemberID"

and i fetch the member id from the following statement
gvDependents.DataKeys[Convert.ToInt32 (e.CommandArgument)].Value.ToString ()
0
 

Author Comment

by:kplonk
ID: 22696845
I do not have the "CommandName" as part of the variable e i am using asp 2.0 under vs 2005:

hers the code

protected void GridViewLights_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        switch ((sender as GridView).ID)
        {
            case "GridViewLights_RowDeleting":
                switch (e.CommandName)
                {
                    case "Delete":
                        //find the row in the datasource
                        // delete the row
                        // accept the datatable changes
                        // rebind the grid
                        break;
                }
                break;
        }

    }

0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 50 total points
ID: 22696852
Sorry add the code that i gave in the GridView's RowCommand event
sorry for the confusion

Leave the RowDeleting event as it is
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question