October 12, 2007

Selecting Checkboxes inside the GridView Control:


How to select the checkboxes inside the Gridviewcontrol


After seraching many forums and websites i have developed my project by using this code.
Now in the button click event write this code which i have attached in blog as image:


// StringBuilder object

StringBuilder str = new StringBuilder();

// Select the checkboxes from the GridView control

for (int i = 0; i < GridView1.Rows.Count; i++)

{

GridViewRow row = GridView1.Rows[i];

bool isChecked = ((CheckBox) row.FindControl("chkBox")).Checked;

if (isChecked)

{

// Column 2 is the name column

str.Append(GridView1.Rows[i].Cells[2].Text);

}

}

// prints out the result

Response.Write(str.ToString());



Note:1.If u want to have StringBuilder then u have to include the namespace Using.System.Text
2.If u want to delete the datas after getting the Id then u should rewrite the code as
str.Append(GridView1.Rows[i].Cells[2].Text+",");
so that u will get the output.For ex:1,2,(inorder to delete from database)
other wise if u use
str.Append(GridView1.Rows[i].Cells[2].Text);
u will get the output as 12.

0 comments: