Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to delete checkbox on delete button click event..but i cant get id to delete
appropriate checkbox..please help
code is given bellow..

thanx in advance.
C#
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\rAvIrAj\Downloads\Ravi\Ravi\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Grid_Bind();
        }
    }

    public void Grid_Bind()
    {

        SqlCommand cmd = new SqlCommand();

        SqlDataAdapter da = new SqlDataAdapter(); ;

        DataTable dt = new DataTable();
        try
        {
            cn.Open();

            cmd.CommandText = "Select * from Student_Master";

            cmd.Connection = cn;

            cmd.CommandType = CommandType.Text;

            da.SelectCommand = cmd;

            da.Fill(dt);

            gv1.DataSource = dt;

            gv1.DataBind();

            cn.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }
 
 
 
    protected void chk1_CheckedChanged1(object sender, EventArgs e)
    {
        try
        {
            if (((CheckBox)gv1.HeaderRow.FindControl("chk1")).Checked==true)
            {
                foreach (GridViewRow row in gv1.Rows)
                {
                    ((CheckBox)row.FindControl("chk2")).Checked = true;

                }
            }
            else
            {
                foreach (GridViewRow row in gv1.Rows)
                {
                    ((CheckBox)row.FindControl("chk2")).Checked = false;

                }

            }
        }

        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
       

    }

    protected void btn_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gv1.Rows)
        {
           
            if (((CheckBox)row.FindControl("chk2")).Checked == true)
            {
                int id =

                SqlCommand cmd = new SqlCommand("delete from Student_Master where id="+id,cn);
                Grid_Bind();
                ll.Text = "successful";
            }
        }
    }
Posted
Updated 15-Jul-12 17:49pm
v2

try this.

C#
//num is the index of the id column ex. gv1.Rows(i).Cells(1).Text
string id = gv1.Rows(i).Cells(num).Text
 
Share this answer
 
you can used id like this:-
int id=
int.parse(gv1.Rows(index).Cells(num).Text.ToString());


index is int type have column no index.like id,name,s_name
than asign index is 0
<pre></pre>

i hope your problem will solve if not please give comment.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900