Delete rows in Gridview

Discussion in 'C Programming' started by PetroTiburcio, Jun 3, 2013.

  1. PetroTiburcio

    PetroTiburcio

    Joined:
    May 17, 2013
    Messages:
    7
    Likes Received:
    0
    hi, I am having a problem with deleting gridview rows. I am populating my gridview by selecting value in a dropdownlist then by clicking the add button, value will be added in my gridview, here is my code:

    Code (Text):
    <asp:GridView ID="GridView1" runat="server"
                    CssClass="mGrid" EmptyDataText = "There are no records to display">
                    <Columns>
                        <asp:TemplateField ItemStyle-Width="10">
                <HeaderTemplate>
     
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server"/>
                </ItemTemplate>
            </asp:TemplateField>
     
                    </Columns>
    </asp:GridView>
    in my code behind:

    Code (Text):
    protected void Page_Load(object sender, EventArgs e)
        {
          if (!IsPostBack)
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                DataColumn dc = new DataColumn("Id");
                DataColumn dc1 = new DataColumn("Name");
                //DataColumn dc2 = new DataColumn("Id");
     
                dt.Columns.Add(dc);
                dt.Columns.Add(dc1);
               // dt.Columns.Add(dc2);
                ds.Tables.Add(dt);
                Session["data"] = ds;
                GridView1.DataBind();
            }
        }
     
    protected void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=GATE-PC\\SQLEXPRESS;Initial Catalog=dbProfile;Integrated Security=True");
            con.Open();
     
            foreach (GridViewRow row in GridView1.Rows)
            {
                SqlCommand cmdd = new SqlCommand("Insert into Profile (Id, profile_Id)VALUES(@id, @pid)", con);
                cmdd.CommandType = System.Data.CommandType.Text;
                cmdd.Parameters.AddWithValue("@id", row.Cells[1].Text);
                cmdd.Parameters.AddWithValue("@pid", txtbid.Text);
                cmdd.ExecuteNonQuery();
            }
        }
     
    protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataSet ds = (DataSet)Session["data"];
            DataRow dr = ds.Tables[0].NewRow();
            dr[0] = DropDownList1.Text.Trim();
            dr[1] = DropDownList1.SelectedItem;
            //dr[2] = txtId.Text.Trim();
            ds.Tables[0].Rows.Add(dr);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    Now, I am trying to remove checked rows on my gridview with this code:

    Code (Text):
    protected void btnRemove_Click(object sender, EventArgs e)
        {
            ArrayList del = new ArrayList();
     
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkDelete = (CheckBox)row.Cells[0].FindControl("CheckBox1");
     
                    if (chkDelete != null)
                    {
                        if (chkDelete.Checked)
                        {
     
                            string id = row.Cells[1].Text;
                            del.Add(productId);
                        }
                    }
                }
            }
            GridView1.DataBind();
        }
    With the btnRemove codes above, If I clicked it, it'll remove all values in my gridview even the unchecked rows, what I want is that just the checked rows not all. And are there any other simple way of removing rows in a gridview than using checkbox? I am using c# with asp.net. Thanks in advance and God Bless.
     
    PetroTiburcio, Jun 3, 2013
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Subba Rao via DotNetMonster.com

    script for moving rows up and down and traverse thru rows of HTML table

    Subba Rao via DotNetMonster.com, Mar 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    9,231
    Subba Rao via DotNetMonster.com
    Mar 19, 2005
  2. MC
    Replies:
    2
    Views:
    8,757
  3. =?Utf-8?B?TWFya0F1cml0?=

    GRIDVIEW: Effecting buttonfields on single gridview rows

    =?Utf-8?B?TWFya0F1cml0?=, May 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    660
    =?Utf-8?B?TWFya0F1cml0?=
    May 23, 2006
  4. Michael
    Replies:
    2
    Views:
    1,853
    Michael
    Apr 30, 2008
  5. Andy B
    Replies:
    0
    Views:
    632
    Andy B
    Aug 8, 2008
  6. Astrith

    How to delete Rows of a GridView?

    Astrith, Sep 26, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    854
    Bogdan
    Sep 26, 2008
  7. VijayRama
    Replies:
    2
    Views:
    2,735
    Gregory A. Beamer
    Oct 12, 2009
  8. MC

    Gridview returns old value of gridview.rows.count!

    MC, Apr 26, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    469
Loading...