Solved

Delete row from Gridview where the datasource is a datatable

Posted on 2008-10-08
5
751 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now