Thursday, 29 December 2011

CheckBoxex in gridview

This tutorial shows using checkboxes in gridview, checking all checkboxes, deleting checked records, aproved checked records


Step1. Aspx.code














< asp:BoundField>







< asp:BoundField>



< asp:BoundField>



HeaderTemplate>

< ItemTemplate>
< asp:TemplateField>
Columns>







< asp:GridView>







Step1.cs code

For delete button





protected void butDel_Click(object sender, EventArgs e)
{
DataAccess da = new DataAccess();
da.OpenConnection();
try
{
foreach (GridViewRow row in GridView3.Rows)
{
CheckBox chkDelete = (CheckBox)row.FindControl("chkSelect");
if (chkDelete.Checked)
{
string rrid = row.Cells[1].Text;
da.ExecuteNonQuery("delete from Eprdetail where rrid='" + rrid + "'");
}
}
}




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




For Aprove button












protected void butapp_Click(object sender, EventArgs e)
{
string rid="0";
string rrid="0";
DataAccess da = new DataAccess();
da.OpenConnection();
try
{
int indexcount = GridView3.Rows.Count;
int index = 0;
foreach (GridViewRow row in GridView3.Rows)
{
//get checkBox from gridview
CheckBox chkDelete = (CheckBox)row.FindControl("chkSelect");
if (chkDelete.Checked) //checking checkboxes
{
rrid = row.Cells[1].Text;
da.ExecuteNonQuery(" update EpinRequest set status='Approve' where rid='"+rid+"'");
}
}
Server.Transfer("ApproveEpins.aspx");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}


Method for selecting all checkBoxes












protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkAll =
(CheckBox)GridView3.HeaderRow.FindControl("chkSelectAll");





if(chkAll.Checked == true)
{
foreach(GridViewRow gvRow in GridView3.Rows)





{
CheckBox chkSel =
(CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = true;

}
}
else
{
foreach (GridViewRow gvRow in GridView3.Rows)
{
CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = false;
}
}





}

No comments:

Post a Comment