locked
GridView Edit Update and Delete RRS feed

  • Question

  • User-672449838 posted

    I Remember at one point in my project when I bound a grid view to a data source I was able to check some boxes for edit, update, and delete. I now have my grid view nested on 1 page displaying results, but from an admin point of view, I want to be able to edit update and delete textbook information. So I created a separate page called "Edit text books" and made a grid view, bound the source to my Books table and now that I want to select the check boxes for edit update and delete it isn't an option? why?

    Wednesday, August 24, 2016 3:35 AM

Answers

  • User-707554951 posted

    Hi Norque,

    From your description, do you mean you want to use checkbox to implement Edit/Delete operation, when checked the check box you want to redirect to the Edit page to edit the selected row information? If that is the case, you could do as the code below:

    asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                           Edit <asp:CheckBox ID="CheckBox1" runat="server"  OnCheckedChanged="CheckBox1_CheckedChanged"  AutoPostBack="true"/>
                           Delete<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged"  AutoPostBack="true"/>
                   
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="ID" HeaderText="ID"/>            
                    <asp:TemplateField HeaderText="Link">
                      <ItemTemplate>
                          <asp:LinkButton ID="LinkButton1" runat="server" data-name='<%#Eval("Name") %>' PostBackUrl='<%#Eval("Link") %>' OnClientClick="checkrecords(this)"><%#Eval("Link") %></asp:LinkButton>
                      </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    

    CodeBehind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ID", typeof(int)), new DataColumn("Name", typeof(string)),
                new DataColumn("Link", typeof(string)) });
                    dt.Rows.Add(1, "Asp.net Forum", "http://forums.asp.net/");
                    dt.Rows.Add(2, "Goole", "https://www.google.com.sg/");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
    
    
            }
            protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
                CheckBox cb = (CheckBox)sender;
                //Get the row  current CheckBox located
                GridViewRow row = (GridViewRow)cb.NamingContainer;
                //Determine the RowIndex of the Row whose Button was clicked.
                int index = row.RowIndex;
                //Get the value of column from the DataKeys using the RowIndex.
                int id = Convert.ToInt32(GridView1.DataKeys[index].Values[0]);
                if (cb.Checked == true)
                {
                    //redict to 'Edit text books' page with passing id  
                }
            }
            protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
            {
                CheckBox cb = (CheckBox)sender;
                //Get the row  current CheckBox located
                GridViewRow row =(GridViewRow)cb.NamingContainer;
                //Determine the RowIndex of the Row whose Button was clicked.
                int index = row.RowIndex;
                //Get the value of column from the DataKeys using the RowIndex.
                int id = Convert.ToInt32(GridView1.DataKeys[index].Values[0]);
                if (cb.Checked == true)
                {
                    //do operation to delete the row from GridView according to id
                }
            }
    

    Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.

    Best regards
    Cathy

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 25, 2016 8:08 AM