locked
how can i delet the checked rows from gridview in asp.net RRS feed

  • Question

  • User-1092307275 posted

    I have a gridview that has a checkbox in it. now I want to delete the checked row  from my grid

    how can ido this correctly?

    Sunday, December 30, 2018 7:13 AM

Answers

  • User283571144 posted

    Hi dorsa2,

    According to your description, you could create a loop to get each checked checkbox rule in the girdview, then you could get the   primary key according to the row number.At last, you could delete the row according to the row number.More details, you could refer to the following code.

    aspx:

      <script type="text/javascript">
            function del()
            {
                var c = confirm("do you want to delete it?");
                if (c)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        </script>
     
    <form id="form1" runat="server">
            <div>
                 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox1" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Subject" HeaderText="Subject" />
                        <asp:BoundField DataField="Description" HeaderText="Description" />
                        <asp:BoundField DataField="Start_Date" HeaderText="Start_Date" />
                        <asp:BoundField DataField="End_Date" HeaderText="End_Date" />
                        <asp:BoundField DataField="ThemeColor" HeaderText="ThemeColor"/>
                    </Columns>
                </asp:GridView>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" />
            </div>
        </form>
    

    Code-behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    getData();
                    Button1.Attributes.Add("onclick", "javascript:return del()");
                }
     
            }
     
            protected void getData()
            {
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=lilydata;Integrated Security=True";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    SqlDataAdapter sda = new SqlDataAdapter("select * from work", connect);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    this.GridView1.DataSource = dt;
                    this.GridView1.DataBind();
                }
     
            }
     
            protected void deleteR(string subject)
            {
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=lilydata;Integrated Security=True";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    string sql = "delete from work where Subject=@sub";
                    using (SqlCommand cmd = new SqlCommand(sql, connect))
                    {
                        cmd.Parameters.AddWithValue("@sub", subject);
                        cmd.ExecuteNonQuery();
                        connect.Close();
                    }
                }
            }
     
            protected void Button1_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow r in GridView1.Rows)
                {
                    CheckBox cb = (CheckBox)r.FindControl("CheckBox1");
                    if (cb.Checked)
                    {
                        string subject = Convert.ToString(r.Cells[1].Text);
                        deleteR(subject);
                    }
                }
                getData();
            }
     
    

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 31, 2018 7:04 AM

All replies

  • User-943250815 posted

    Considering you have a button to delete records with checkbox checked, and Gridview has a RecordID in Datakeys, you can use this sample on button click

    For Each zRow As GridViewRow In MyGrid.Rows
      If zRow.RowType = DataControlRowType.DataRow Then
         zChkBoxItem = CType(zRow.Cells(0).FindControl("chkItem"), CheckBox)
         If zChkBoxItem.Checked Then
           Dim zRecID As Integer = MyGrid.DataKeys(zRow.RowIndex).Value
           ' do whatever you need here
         End If
      End If
    Next

    Sunday, December 30, 2018 4:03 PM
  • User283571144 posted

    Hi dorsa2,

    According to your description, you could create a loop to get each checked checkbox rule in the girdview, then you could get the   primary key according to the row number.At last, you could delete the row according to the row number.More details, you could refer to the following code.

    aspx:

      <script type="text/javascript">
            function del()
            {
                var c = confirm("do you want to delete it?");
                if (c)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        </script>
     
    <form id="form1" runat="server">
            <div>
                 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox1" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Subject" HeaderText="Subject" />
                        <asp:BoundField DataField="Description" HeaderText="Description" />
                        <asp:BoundField DataField="Start_Date" HeaderText="Start_Date" />
                        <asp:BoundField DataField="End_Date" HeaderText="End_Date" />
                        <asp:BoundField DataField="ThemeColor" HeaderText="ThemeColor"/>
                    </Columns>
                </asp:GridView>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" />
            </div>
        </form>
    

    Code-behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    getData();
                    Button1.Attributes.Add("onclick", "javascript:return del()");
                }
     
            }
     
            protected void getData()
            {
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=lilydata;Integrated Security=True";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    SqlDataAdapter sda = new SqlDataAdapter("select * from work", connect);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    this.GridView1.DataSource = dt;
                    this.GridView1.DataBind();
                }
     
            }
     
            protected void deleteR(string subject)
            {
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=lilydata;Integrated Security=True";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    string sql = "delete from work where Subject=@sub";
                    using (SqlCommand cmd = new SqlCommand(sql, connect))
                    {
                        cmd.Parameters.AddWithValue("@sub", subject);
                        cmd.ExecuteNonQuery();
                        connect.Close();
                    }
                }
            }
     
            protected void Button1_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow r in GridView1.Rows)
                {
                    CheckBox cb = (CheckBox)r.FindControl("CheckBox1");
                    if (cb.Checked)
                    {
                        string subject = Convert.ToString(r.Cells[1].Text);
                        deleteR(subject);
                    }
                }
                getData();
            }
     
    

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 31, 2018 7:04 AM
  • User-1092307275 posted

    Thx Dear Brando ZWZ,

    I do What you say But I encountered an error

    Monday, December 31, 2018 8:28 AM
  • User-1092307275 posted

    I remove the form control from my HTML code and my problem solved.

    now when I run my <g class="gr_ gr_372 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="372" data-gr-id="372">prj</g> it just shows the info of the grid and when I want to delete from the grid, after shows msg box it just unchecks the checkbox And does not do anything else

     

    Monday, December 31, 2018 8:47 AM
  • User-943250815 posted

    @dorsa2, Attention to Exception Message. A page can have only one server-side Form tag.
    Seems you had more than one Form tag and deleted all. Form tag should be on page, but only one

    Monday, December 31, 2018 12:27 PM
  • User283571144 posted

    Hi dorsa2,

    As far as I know, if you don't delete the rows in the grdiview, I guess you use the wrong sql query.

    Could you please share the code-behind you have used in your application?

    If you could post more details information, it will be more easily for us to reproduce the issue and find out the solution.

    Best Regards,

    Brando

    Wednesday, January 2, 2019 1:23 AM