在GripView中如何在每行前加CheckBox,求助!
在VS2005平台上用C#,在新建设备资料网页中,需输入及选择数据,最后确定保存。有个GripView,如何在其中每行前加入CheckBox,最后按本网页确定键后,如何获得选中行的数据?可选中多行,请指教,谢谢!
------解决方案--------------------随便搜了4个例子:
http://www.cnblogs.com/eddie005/archive/2006/07/04/442837.aspx
http://blog.csdn.net/net_lover/archive/2006/10/25/1351234.aspx
http://www.dwww.cn/News/2007-7/2007726178115245.shtml
http://hugh-lin.cnblogs.com/archive/2005/08/10/211549.html
自己学着google吧。
------解决方案--------------------GridView加checkbox与DATAGRID加checkbox没什么区别
无非用个模板列,放个checkbox,再用一段JS脚本控制checkbox的值
提交的时候,再从gridview上获取checkbox,判断checkbox.checked的值就可以知道,哪条记录被选中了,思路基本上是这样。
------解决方案--------------------建议继承GridView
以下代码添加一个模板列 包含一个checkbox
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
TemplateField field = this.Columns[0] as TemplateField;
if (field == null || !(field.ItemTemplate is SelectField))
{
this.Columns.Insert(0, SelectField.CreateSelectField( "KeyValue "));
}
}
以下是SelectField.cs
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FFCS.BNet.Controls
{
/// <summary>
/// 选择域
/// </summary>
[ToolboxData( " <{0}:SelectField runat=server> </{0}:SelectField> ")]
public class SelectField : DataControlField, IBindableTemplate, ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string _dataField;
/// <summary>
/// 构造函数
/// </summary>
/// <param name= "type "> 类型 </param>
/// <param name= "colname "> 名称 </param>
/// <param name= "dataField "> 数据域 </param>
public SelectField(DataControlRowType type, string colname, string dataField)
{
templateType = type;
columnName = colname;
_dataField = dataField;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name= "type "> 类型 </param>
/// <param name= "colname "> 名称 </param>
public SelectField(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}
/// <summary>
/// 工厂 创建 SelectField
/// </summary>
/// <returns> DataControlField </returns>
protected override DataControlField CreateField()
{
return new SelectField(DataControlRowType.DataRow, " ");
}
/// <summary>
/// 数据绑定
/// </summary>
/// <param name= "sender "> 发送者 </param>
/// <param name= "e "> 事件参数 </param>
private void checkBox_DataBinding(object sender, EventArgs e)
{