?
Solved

Delete row from Gridview where the datasource is a datatable

Posted on 2008-10-08
5
Medium Priority
?
791 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

771 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