最近,因为需要用到在DATAGRID 中放置一些CheckBox 来判断是否选中某一行。本来可以使用javascript来实现。不过使用脚本实现,调试起来十分不方便,因此做一个后台代码的CheckBox 列来控制。
需要制作自定义功能的DATAGRID 列,需要继承DataGridColumn 这个类。然后重写 InitializeCell 这个方法来进行对这个自定义的列进行控制。代码中包含了记录选中的项,和没有被选中的项,并且可以在一个DataGrid中同时使用多个这样的CheckBox 列,只需要使用时通过ID属性区分就可以。
代码如下:
这里只是继承DataGridColumn 重写InitializeCell函数的部分。
如何使用的代码片段
<dgctl:CheckBoxColumn HeaderText="用户名" ID="chkUserName">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</dgctl:CheckBoxColumn>
<dgctl:CheckBoxColumn HeaderText="用户详细信息" ID="chkUserDetail">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</dgctl:CheckBoxColumn>
完整代码下载
需要制作自定义功能的DATAGRID 列,需要继承DataGridColumn 这个类。然后重写 InitializeCell 这个方法来进行对这个自定义的列进行控制。代码中包含了记录选中的项,和没有被选中的项,并且可以在一个DataGrid中同时使用多个这样的CheckBox 列,只需要使用时通过ID属性区分就可以。
代码如下:
1
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
2
{
3
4
//let the base class initialize the cell
5
base.InitializeCell(cell, columnIndex, itemType);
6
7
//we don't want to add a checkbox to the header.
8
if(itemType == ListItemType.EditItem || itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem || itemType == ListItemType.SelectedItem){
9
10
HtmlInputCheckBox checkbox = new HtmlInputCheckBox();
11
//assign an ID that we can use to find the control later
12
if(_strId==String.Empty)
13
{
14
checkbox.ID = "checkboxCol";
15
}
16
else
17
{
18
checkbox.ID=myID;
19
}
20
21
cell.Controls.Add(checkbox);
22
23
}
24
25
}
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType) 2
{3
4
//let the base class initialize the cell5
base.InitializeCell(cell, columnIndex, itemType);6

7
//we don't want to add a checkbox to the header.8
if(itemType == ListItemType.EditItem || itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem || itemType == ListItemType.SelectedItem){9

10
HtmlInputCheckBox checkbox = new HtmlInputCheckBox();11
//assign an ID that we can use to find the control later12
if(_strId==String.Empty)13
{14
checkbox.ID = "checkboxCol";15
}16
else17
{18
checkbox.ID=myID;19
}20
21
cell.Controls.Add(checkbox);22

23
}24

25
}这里只是继承DataGridColumn 重写InitializeCell函数的部分。
如何使用的代码片段
<dgctl:CheckBoxColumn HeaderText="用户名" ID="chkUserName">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</dgctl:CheckBoxColumn>
<dgctl:CheckBoxColumn HeaderText="用户详细信息" ID="chkUserDetail">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</dgctl:CheckBoxColumn>完整代码下载


浙公网安备 33010602011771号