Update SQL Server from GridView with a Checkbox
There is a GridView with a Checkbox field on the page. And there is a button on the same page. There is a fie开发者_如何学Pythonld which is related to the checkbox in the sql. Is there any simple way to update the sql? Not row by row.
Best Regards,
You can use the following snippet to return the values of all selected checkboxes as a comma-separated string:
string values = string.Empty;
foreach (ListItem item in checkboxList.Items)
{
if (item.Selected)
values += item.Value + ",";
}
You can use this value in SQL as follows:
string sql = String.Format("UPDATE table SET selected = 1 WHERE ID IN ({0})", values );
If you are editing each row individually I'm afraid there's not other way that updating row by row or you can have a batch update for each group of values for example:
Update MyTable Set checkbox=1 WHERE Id IN(list of ids);
Update MyTable Set checkbox=0 WHERE Id IN(list of ids);
If you'd like to update the same value for all the rows that are shown you can do this:
Update MyTable set checkbox=value WHERE your whare cluase of showing fields
精彩评论