?
Solved

Delete row from Gridview where the datasource is a datatable

Posted on 2008-10-08
5
Medium Priority
?
804 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

750 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