Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:55:05+00:00 2026-06-06T22:55:05+00:00

This seems like a repeated question but i’m not able to get my answer.

  • 0

This seems like a repeated question but i’m not able to get my answer.

I have a grid view and I need to delete a particular row, when I click on a button outside the gridview.

 protected void btnDelete_Click(object sender, EventArgs e)
      {
            dtable = (DataTable)Session["data"];
            DataRow row = dtable.Rows[DataGV1.SelectedIndex];
            dtable.Rows.Remove(row);
            DataGV1.DataSource = dtable;
            DataGV1.DataBind();
            Session["data"] = dtable;
        }

The session variable has the previous state of datatable.

protected void DataGV1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridView _gridView = (GridView)sender;

        // Get the selected index 
         _selectedIndex = int.Parse(e.CommandArgument.ToString());
     }

Gridview controls

        onselectedindexchanged="DataGV1_SelectedIndexChanged" 
        OnRowCommand="DataGV1_RowCommand" OnRowDeleting="DataGV1_RowDeleting"
        AutoGenerateSelectButton="False" DataKeyNames="Role,Last_name">
        <Columns>
            <asp:ButtonField DataTextField="last_name" HeaderText="Last_name" CommandName="SingleClick"
                SortExpression="last_name" Text="Button" />
            <asp:BoundField DataField="role" HeaderText="role" SortExpression="role" />
             <asp:BoundField DataField="role" HeaderText="role"                      HeaderText="Frist_name" 
                 SortExpression="first_name" Text="First_name" />
        </Columns>
    </asp:GridView>

This doesn’t seem to work.
Can u please tell me where I am going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:55:06+00:00Added an answer on June 6, 2026 at 10:55 pm

    If button is outside the GridView then no need to handle RowCommand event (In fact it is inappropriate).

    Suggestion:

    You have to add a TemplateField column, drop the CheckBox control in ItemTemplate of TemplateField and write code in click handler of button to traverse the GridView.Rows collection, identify the selected row by reading value of CheckBox control and perform deletion action if that CheckBox is checked.

    Demo DataSource (List<T>)

    public class Item
    {
        public int ID { get; set; }
        public string Name { get; set; }
    
        public static List<Item> Data()
        {
            List<Item> list = new List<Item>()
            {
                    new Item(){ ID=11, Name="A"},
                    new Item(){ ID=12, Name="B"},
                    new Item(){ ID=13, Name="C"},
                    new Item(){ ID=14, Name="D"},
                    new Item(){ ID=15, Name="E"},
            };
            return list;
        }
    }
    

    Markup:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
       <Columns>
            <asp:ButtonField DataTextField="Name" HeaderText="Name" CommandName="SingleClick"
                SortExpression="last_name" Text="Button" />
            <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
        </Columns>
    </asp:GridView>
    <asp:Button ID="btnDelete" runat="server" Text="Button" />
    

    Code-behind (Page_Load)

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["data"] = Item.Data();
            GridView1.DataSource = Session["data"];
            GridView1.DataBind();
        }
        /*--- RowCommand handler ---*/
        GridView1.RowCommand += (sa, ea) =>
            {
                ViewState["RowIndex"] = ea.CommandArgument.ToString();
            };
        /*--- Delete button click handler ---*/
        btnDelete.Click += (sa, ea) =>
            {
                if (ViewState["RowIndex"] != null)
                {
                    int index = int.Parse(ViewState["RowIndex"].ToString());
                    List<Item> items = Session["data"] as List<Item>;
                    items.RemoveAt(index);
                    GridView1.DataSource = Session["data"];
                    GridView1.DataBind();
                    ViewState["RowIndex"] = null;
                }
            };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a basic question, but I haven't found any clear answers. Essentially,
This seems like a basic question, but it's still bugging me. Why doesn't MyObject
Sorry it seems like a repeated question, BUT I think I don't qualify to
OK, this feels like a question that should be easy to answer, but as
This seems like it should be obvious but I can't figure it out. Suppose
This seems like it should be something I already know. We need to run
This seems like it should be something very easy to do, but every time
This seems like it should be an easy thing to do, but for the
This seems like it should be really simple, but I'm unable to figure this
This seems like it would be fairly simple, but I've been unable to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.