问题描述
我想用checkboxlist多选去控制gridview显示,却不但不能实现多选,而且有时还出错:就是在数据库中测试查询到的数据行不是显示到GRIDVIEW图标中的数据行?checkboxllist和gridlist都是用控件去绑定数据库的?水平不高,请指教!
解决方案
解决方案二:
问题表达不清楚。
解决方案三:
CheckBox多选绑定GridView显示,实现批量删除和更新信息示例//GridView设置<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"OnRowDataBound="GridView1_RowDataBound"OnSelectedIndexChanging="GridView1_SelectedIndexChanging"Font-Size="9pt"AllowPaging="True"EmptyDataText="没有相关数据可以显示!"OnPageIndexChanging="GridView1_PageIndexChanging"CellPadding="4"ForeColor="#333333"GridLines="None"><Columns><asp:TemplateField><ItemTemplate><asp:CheckBoxID="cbSingleOrMore"runat="server"/></ItemTemplate></asp:TemplateField><asp:BoundFieldDataField="id"HeaderText="信息ID"/><asp:BoundFieldDataField="name"HeaderText="信息主题"/><asp:BoundFieldDataField="type"HeaderText="信息分类"/><asp:BoundFieldDataField="content"HeaderText="发布内容"/><asp:BoundFieldDataField="userName"HeaderText="发布人"/><asp:BoundFieldDataField="lineMan"HeaderText="联系人"/><asp:BoundFieldDataField="issueDate"HeaderText="发布时间"DataFormatString="{0:d}"/></Columns><FooterStyleBackColor="#990000"Font-Bold="True"ForeColor="White"/><RowStyleBackColor="#FFFBD6"ForeColor="#333333"/><SelectedRowStyleBackColor="#FFCC66"Font-Bold="True"ForeColor="Navy"/><PagerStyleBackColor="#FFCC66"ForeColor="#333333"HorizontalAlign="Right"/><HeaderStyleBackColor="#990000"Font-Bold="True"ForeColor="White"/><AlternatingRowStyleBackColor="White"/></asp:GridView>//CheckBox设置<asp:CheckBoxID="cbAll"runat="server"AutoPostBack="True"Font-Size="9pt"OnCheckedChanged="cbAll_CheckedChanged"Text="全选/反选"/><asp:ButtonID="btnDeleteMore"runat="server"Font-Size="9pt"Text="部分删除或全部删除"OnClick="btnDeleteMore_Click"onload="btnDeleteMore_Load"Width="134px"/></td><asp:ButtonID="btnRre"runat="server"Font-Size="9pt"Text="取消部分或全部选择"OnClick="btnRre_Click"Width="126px"/><asp:ButtonID="btnUpdateTime"runat="server"onclick="btnUpdateTime_Click"Text="更新发布时间"/>//cs页面功能设置SqlConnectionsqlcon;stringstrCon=ConfigurationManager.AppSettings["conStr"];protectedvoidPage_Load(objectsender,EventArgse)//加载绑定数据显示{if(!IsPostBack){this.GV_DataBind();}}publicvoidGV_DataBind(){stringsqlstr="select*fromtb_inf";sqlcon=newSqlConnection(strCon);SqlDataAdapterda=newSqlDataAdapter(sqlstr,sqlcon);DataSetds=newDataSet();sqlcon.Open();da.Fill(ds,"tb_inf");sqlcon.Close();this.GridView1.DataSource=ds;this.GridView1.DataKeyNames=newstring[]{"id"};this.GridView1.DataBind();if(GridView1.Rows.Count>0){return;//有数据,不要处理}else//显示表头并显示没有数据的提示信息{StrHelper.GridViewHeader(GridView1);}}//codego.net/tags/11/1///选择CheckBoxprotectedvoidcbAll_CheckedChanged(objectsender,EventArgse){for(inti=0;i<=GridView1.Rows.Count-1;i++){CheckBoxcbox=(CheckBox)GridView1.Rows[i].FindControl("cbSingleOrMore");if(cbAll.Checked==true){cbox.Checked=true;}else{cbox.Checked=false;}}}//删除功能protectedvoidbtnDeleteMore_Click(objectsender,EventArgse){sqlcon=newSqlConnection(strCon);//创建数据库连接SqlCommandsqlcom;//创建命令对象变量for(inti=0;i<=GridView1.Rows.Count-1;i++)//循环遍历GridView控件每一项{CheckBoxcbox=(CheckBox)GridView1.Rows[i].FindControl("cbSingleOrMore");//查找嵌套在GridView控件中的单选框if(cbox.Checked==true)//如果操作为选中状态{stringstrSql="deletefromtb_infwhereid=@id";//定义带参数的删除语句if(sqlcon.State.Equals(ConnectionState.Closed))sqlcon.Open();//打开数据加连接sqlcom=newSqlCommand(strSql,sqlcon);//创建执行删除操作的命令对象SqlParameterprame=newSqlParameter("@id",SqlDbType.Int,4);//定义参数sqlcom.Parameters.Add(prame);//添加参数sqlcom.Parameters["@id"].Value=GridView1.DataKeys[i].Value.ToString();//参数赋值if(sqlcom.ExecuteNonQuery()>0)//判断删除是否成功{StrHelper.Alert("删除成功!");}else{StrHelper.Alert("删除失败!");}sqlcon.Close();//关闭数据库连接}}GV_DataBind();//重新绑定数据控件}//更新数据protectedvoidbtnUpdateTime_Click(objectsender,EventArgse){sqlcon=newSqlConnection(strCon);//创建数据库连接SqlCommandsqlcom;//创建命令对象变量for(inti=0;i<=GridView1.Rows.Count-1;i++)//循环遍历GridView控件每一项{CheckBoxcbox=(CheckBox)GridView1.Rows[i].FindControl("cbSingleOrMore");if(cbox.Checked==true){stringstrSql="Updatetb_infsetissueDate=@UpdateTimewhereid=@id";if(sqlcon.State.Equals(ConnectionState.Closed))sqlcon.Open();//打开数据库连接sqlcom=newSqlCommand(strSql,sqlcon);SqlParameter[]prams={newSqlParameter("@UpdateTime",SqlDbType.DateTime),newSqlParameter("@id",SqlDbType.Int,4)};prams[0].Value=DateTime.Now;prams[1].Value=GridView1.DataKeys[i].Value;foreach(SqlParameterparameterinprams){sqlcom.Parameters.Add(parameter);}if(sqlcom.ExecuteNonQuery()>0){StrHelper.Alert("更新成功!");}else{StrHelper.Alert("更新失败!");}sqlcon.Close();}}GV_DataBind();}//执行索引操作protectedvoidGridView1_SelectedIndexChanging(objectsender,GridViewSelectEventArgse){stringid=this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();sqlcon=newSqlConnection(strCon);SqlCommandcom=newSqlCommand("select[check]fromtb_infwhereid='"+id+"'",sqlcon);sqlcon.Open();stringcount=Convert.ToString(com.ExecuteScalar());if(count=="False"){count="1";}else{count="0";}com.CommandText="updatetb_infset[check]="+count+"whereid="+id;com.ExecuteNonQuery();sqlcon.Close();this.GV_DataBind();}