Thursday, April 24, 2008

Selecting checkbox in gridview and select values from gridview rows





CheckBox ch = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox1");
if (ch.Checked == true)
{
foreach (GridViewRow gv in GridView1.Rows)
{
ch2 = (CheckBox)gv.FindControl("CheckBox2");

ch2.Checked = true;
string workordernum = gv.Cells[1].Text;
string name = gv.Cells[2].Text;
string dept = gv.Cells[3].Text;
string workorder = gv.Cells[4].Text;
string equipment = gv.Cells[5].Text;
string worktype = gv.Cells[6].Text;
con.Open();
Label6.Text = "insert into jobapprove values('" + Label5.Text.ToString() + "','" + workordernum.ToString() + "','" + name.ToString() + "','" + dept.ToString() + "','" + workorder.ToString() + "','" + equipment.ToString() + "','" + worktype.ToString() + "')";
SqlCommand cmdd = new SqlCommand("insert into jobapprove values('" + Label5.Text.ToString() + "','" + workordernum.ToString() + "','" + name.ToString() + "','" + dept.ToString() + "','" + workorder.ToString() + "','" + equipment.ToString() + "','" + worktype.ToString() + "')", con);
cmdd.ExecuteNonQuery();
con.Close();
}
}
if (ch.Checked == false )
{
foreach (GridViewRow gv in GridView1.Rows)
{
ch2 = (CheckBox)gv.FindControl("CheckBox2");
ch2.Checked = false;
}
}


Coding For selecting Entire Checkbox while selecting Checkbox in Gridview HeaderRow

ch = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox2");
foreach (GridViewRow gr in GridView1.Rows)
{
if (ch.Checked == true)
{
CheckBox ch1 = (CheckBox)gr.FindControl("CheckBox1");
ch1.Checked = true;
}
else if (ch.Checked == false)
{
CheckBox ch1 = (CheckBox)gr.FindControl("CheckBox1");
ch1.Checked = false;
}

}

No comments: