Solved

Delete row from Gridview where the datasource is a datatable

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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