向各位请教如何在datagrid控件的头部加上CheckBox复选框
我想在datagrid控件中加一个模板列,里面放CheckBox,又想做一个全选的复选框,放在datagrid的头部,点击全选就将控件中的所有复选框选中,向各位请教,能不能给出具体实现代码。
------解决方案--------------------easy
------解决方案--------------------public int id;
private void del()
{
CheckBox chk1=new CheckBox ();
for(int i=0;i <this.mygrid .Items .Count ;i++)
{
chk1=(CheckBox)this.mygrid .Items [i].Cells [0].FindControl ( "chkselect ");
if(chk1.Checked )
{
id=Convert.ToInt32 (this.mygrid .Items [i].Cells [1].Text .Trim ());
SqlConnection con=new SqlConnection (System.Configuration .ConfigurationSettings .AppSettings [ "con "]);
con.Open ();
SqlCommand cmd1=new SqlCommand ( "delete from news where newsid= "+this.id ,con);
cmd1.ExecuteNonQuery ();
con.Close ();
bind();
}
}
}
public void bind()
{
SqlConnection con=new SqlConnection (System.Configuration .ConfigurationSettings .AppSettings [ "con "]);
con.Open ();
SqlCommand cmd=new SqlCommand ( "select * from news ",con);
SqlDataReader sdr=cmd.ExecuteReader ();
this.mygrid .DataSource =sdr;
this.mygrid .DataBind ();
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack )
{
bind();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mygrid.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.mygrid_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void mygrid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
switch (e.CommandName )
{
case "selectall ":
CheckBox chk=new CheckBox ();
for(int i=0;i <this.mygrid .Items .Count;i++ )
{
chk=(CheckBox)this.mygrid .Items [i].Cells [0].FindControl( "chkselect ");
chk.Checked =true;
}
break;
case "del ":del();
break;
}
这样就能实现了,记得散分给我:)
------解决方案--------------------js 或者 server 端都可以,
如果只需要做选择,当然采取客户端 js 了
没有 datagrid 的demo 了,提供个 datalist 的给你参考
http://www.cnblogs.com/Jinglecat/archive/2007/05/30/764570.html
------解决方案--------------------HTML部分: <HeaderTemplate> <INPUT id= "chbAllSelect " onclick= "SetAllSelect(this.checked); " type= "checkbox "> </HeaderTemplate>
javascript代码:
function SetAllSelect(isSelected)
{
var dgList = document.all( "dgSympList ");
var intCount = dgList.rows.length, chbSelect;
for (var intIndex=1; intIndex <intCount; intIndex++)
{
chbSelect = document.all( "dgSympList__ctl " + (intIndex + 2) + "_chbSelect ");
if (chbSelect == null)
{
continue;
}
chbSelect.checked = isSelected;