C# .NET - checkbox in gridview - Asked By vasan raju on 26-Jun-12 07:05 AM

when i click checkbox in gridview in asp.net the particular data has to be passed to the appropriate controls.
pls anyone let me know the coding in c Sharp
Lalit M replied to vasan raju on 26-Jun-12 07:15 AM
Check this link will ghelp u;;

http://stackoverflow.com/questions/1932933/post-any-checked-chekbox

Using JQuery
----------
http://jvance.com/blog/2009/07/14/GetValueOfCheckboxUsingJQueryToEnableButton.xhtml
bharti odedra replied to vasan raju on 26-Jun-12 07:18 AM
try this
  protected void Gridviewselectbus_RowCommand(object sender, GridViewCommandEventArgs e)
   
 {
     
 if (e.CommandName == "Up")
     
 {
       
 int index = Convert.ToInt32(e.CommandArgument);
       
 if (index == 0)
       
 {
         
 Label1.Text = "You Cant move recor' Up";
         
 Label1.Visible = true;
         
 return;
       
 }
       
 dt = (DataTable)Session["dt"];
       
 int value = Convert.ToInt32(dt.Rows[index]["Index"].ToString());
       
 dt.Rows[index]["Index"] = Convert.ToInt32(index) - 1;
       
 dt.Rows[index - 1]["Index"] = value;
        dt.DefaultView.Sort = "Index";
       
 dt.AcceptChanges();
       
 dtnew = dt.Copy();
       
 Gridviewselectbus.DataSource = dt;
       
 Gridviewselectbus.DataBind();
       
 dt.AcceptChanges();
       
 for (int i = 0; i <= Gridviewselectbus.Rows.Count - 1; i++)
       
 {
         
 dtnew.Rows[i]["Lname"] = Gridviewselectbus.Rows[i].Cells[0].Text;
         
 dtnew.Rows[i]["Job"] = Gridviewselectbus.Rows[i].Cells[1].Text;
         
 dtnew.Rows[i]["Fname"] = Gridviewselectbus.Rows[i].Cells[2].Text;
         
 dtnew.Rows[i]["Index"] = Gridviewselectbus.Rows[i].Cells[3].Text;
       
 }
       
 Session["dt"] = dtnew;
       
 Label1.Text = string.Empty;
     
 }
     
 if (e.CommandName == "Down")
     
 {
       
 int index = Convert.ToInt32(e.CommandArgument);
       
 dt = (DataTable)Session["dt"];
       
 if (Convert.ToInt16(index + 1) == dt.Rows.Count)
       
 {
         
 Label1.Text = "You Cant move record down";
         
 Label1.Visible = true;
         
 return;
       
 }
       
 int value = Convert.ToInt32(dt.Rows[index]["Index"].ToString());
       
 dt.Rows[index]["Index"] = Convert.ToInt32(dt.Rows[index]["Index"].ToString()) + 1;
       
 dt.Rows[index + 1]["Index"] = value;
       
 dt.AcceptChanges();
       
 dt.DefaultView.Sort = "Index";
       
 dt.AcceptChanges();
       
 dtnew = dt.Copy();
       
 Gridviewselectbus.DataSource = dt;
       
 Gridviewselectbus.DataBind();
       
 dt.AcceptChanges();
       
 for (int i = 0; i <= Gridviewselectbus.Rows.Count - 1; i++)
       
 {
         
 dtnew.Rows[i]["Lname"] = Gridviewselectbus.Rows[i].Cells[0].Text;
         
 dtnew.Rows[i]["Job"] = Gridviewselectbus.Rows[i].Cells[1].Text;
         
 dtnew.Rows[i]["Fname"] = Gridviewselectbus.Rows[i].Cells[2].Text;
         
 dtnew.Rows[i]["Index"] = Gridviewselectbus.Rows[i].Cells[3].Text;
       
 }
       
 Session["dt"] = dtnew;
       
 Label1.Text = string.Empty;
     
 }
   
 }
}

protected void btnDeleteSelected_Click(object sender, EventArgs e)

{

  foreach (GridViewRow row in GridView1.Rows)

  {

 

  if (((CheckBox)row.FindControl("checkBox1")).Checked == true)

  {

    /* Get Table Id From Row */

    string id = GridView1.Rows[row.RowIndex].Cells[0].Text;

 

    /* delete from database */

 

    SqlConnection con = new SqlConnection("Connection String");

    con.Open();

 

    SqlCommand comm = new SqlCommand("delete from TableName where id='" + id + "'", con);

    comm.ExecuteNonQuery();

  }

  }

 

  /* Refresh the Grid*/

}

Jitendra Faye replied to vasan raju on 26-Jun-12 08:20 AM
Try like this=

protected void chkInactivate_OnCheckedChanged(object sender, EventArgs e)
    {
      CheckBox chkStatus = (CheckBox)sender;
      GridViewRow selectedrow = (GridViewRow)chkStatus.NamingContainer;
      Label lblEmpId = (Label)selectedrow.FindControl("lblEmpId");

      string str = lblEmpId.Text;
    }


Try and let me know.
dipa ahuja replied to vasan raju on 26-Jun-12 04:43 PM
1. Take a checkbox inside itemTamplate:
 
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"/>
</ItemTemplate>
 
2. Take a Button to insert checked data from the checkbox:
 
protected void btnTransfer_Click(object sender, EventArgs e)
{
  foreach (GridViewRow row in GridView1.Rows)
  {
  if (((CheckBox)row.FindControl("checkBox1")).Checked == true)
  {
    //Get data of ItemTEmplate of table Fields
    lblname.Text = ((Label)row.FindControl("lblName")).Text;
    lblgender.Text = ((Label)row.FindControl("lblGender")).Text;
 
  }
  }
}