< HTML > < HEAD > < title > DataGridCheckBox </ title > < meta content ="Microsoft Visual Studio .NET 7.1" name ="GENERATOR" > < meta content ="C#" name ="CODE_LANGUAGE" > < meta content ="JavaScript" name ="vs_defaultClientScript" > < meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" > </ HEAD > < body MS_POSITIONING ="GridLayout" > < form id ="Form1" method ="post" runat ="server" > < FONT face ="宋体" > < TABLE id ="Table1" cellSpacing ="1" cellPadding ="1" width ="300" border ="0" > < TR > < TD >< asp:datagrid id ="dgCheckBox" runat ="server" GridLines ="Vertical" CellPadding ="3" BackColor ="White" BorderWidth ="1px" BorderStyle ="None" BorderColor ="#999999" AutoGenerateColumns ="False" > < SelectedItemStyle Font-Bold ="True" ForeColor ="White" BackColor ="#008A8C" ></ SelectedItemStyle > < AlternatingItemStyle BackColor ="#DCDCDC" ></ AlternatingItemStyle > < ItemStyle ForeColor ="Black" BackColor ="#EEEEEE" ></ ItemStyle > < HeaderStyle Font-Bold ="True" ForeColor ="White" BackColor ="#000084" ></ HeaderStyle > < FooterStyle ForeColor ="Black" BackColor ="#CCCCCC" ></ FooterStyle > < Columns > < asp:TemplateColumn > < ItemTemplate > < asp:CheckBox id ="chkExport" runat ="server" ></ asp:CheckBox > </ ItemTemplate > < EditItemTemplate > < asp:CheckBox id ="chkExportON" runat ="server" ></ asp:CheckBox > </ EditItemTemplate > </ asp:TemplateColumn > < asp:BoundColumn DataField ="LastName" HeaderText ="LastName" ></ asp:BoundColumn > < asp:BoundColumn DataField ="FirstName" HeaderText ="FirstName" ></ asp:BoundColumn > < asp:TemplateColumn HeaderText ="City" > < ItemTemplate > < asp:Label id =lblColumn runat ="server" Text ='<%# DataBinder.Eval(Container, "DataItem.City") % > '> </ asp:Label > </ ItemTemplate > </ asp:TemplateColumn > </ Columns > < PagerStyle HorizontalAlign ="Center" ForeColor ="Black" BackColor ="#999999" Mode ="NumericPages" ></ PagerStyle > </ asp:datagrid ></ TD > </ TR > < TR > < TD >< asp:button id ="cmdSelectAll" runat ="server" Text ="全选" ></ asp:button >< asp:button id ="cmdFindSelected" runat ="server" Text ="取得选择的项" ></ asp:button ></ TD > </ TR > < TR > < TD >< asp:label id ="Message" runat ="server" > Label </ asp:label ></ TD > </ TR > </ TABLE > </ FONT > </ form > </ body > </ HTML > private void DataGridDataBind() { //定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的 SqlConnection conn = new SqlConnection("Server=soft5;User id=sa;Pwd=;Database=Northwind"); //创建数据适配器对象 SqlDataAdapter da = new SqlDataAdapter("select top 5 LastName,FirstName,City from Employees",conn); //创建DataSet对象 DataSet ds = new DataSet(); try { //填充数据集 da.Fill(ds,"testTable"); //进行数据绑定 dgCheckBox.DataSource = ds.Tables["testTable"]; dgCheckBox.DataBind(); } catch(Exception error) { //输出异常信息 Response.Write(error.ToString()); } } private void cmdSelectAll_Click( object sender, System.EventArgs e) { System.Web.UI.WebControls.CheckBox chkExport; //如果要全选 if(cmdSelectAll.Text == "全选") { //循环设置DataGrid控件中的项 foreach(DataGridItem oDataGridItem in dgCheckBox.Items) { //建立模板列中CheckBox控件的引用 chkExport = (CheckBox)oDataGridItem.FindControl("chkExport"); //选中 chkExport.Checked =true; } cmdSelectAll.Text = "全消"; } else { foreach(DataGridItem oDataGridItem in dgCheckBox.Items) { //建立模板列中CheckBox控件的引用 chkExport = (CheckBox)oDataGridItem.FindControl("chkExport"); //取消 chkExport.Checked = false; } cmdSelectAll.Text = "全选"; } } private void cmdFindSelected_Click( object sender, System.EventArgs e) { System.Web.UI.WebControls.CheckBox chkExport; String sID; System.Text.StringBuilder strMsg = new System.Text.StringBuilder("选中项的City字段值分别为:<hr color=red>"); //循环取的DataGrid控件中选定项的值 foreach(DataGridItem oDataGridItem in dgCheckBox.Items) { chkExport = (CheckBox)oDataGridItem.FindControl("chkExport"); //如果选中了则取值 if(chkExport.Checked) { sID = ((Label)(oDataGridItem.FindControl("lblColumn"))).Text; strMsg.Append(sID+"<br><hr color=red>"); } } //显示选中项的值 Message.Text = strMsg.ToString(); } 转载于:https://www.cnblogs.com/mjgforever/archive/2006/02/16/331926.html