为DataGrid 写一个 DropDownListColumn
Net下有一个类DataGridColumn
作为 DataGrid 控件的不同列类型的基类。
有关此类型全部成员的列表。请參阅 DataGridColumn 成员。
System.Object
System.Web.UI.WebControls.DataGridColumn
System.Web.UI.WebControls.BoundColumn
System.Web.UI.WebControls.ButtonColumn
System.Web.UI.WebControls.EditCommandColumn
System.Web.UI.WebControls.HyperLinkColumn
System.Web.UI.WebControls.TemplateColumn
我们完毕能够写一个继承DataGirdColumn的类,这样就成了。DropDownListColumn,CheckBoxListColumn等。
控件的代码例如以下。
using System;
using System.Web.UI;
using System.Web.UI.WebControls ;
using System.Collections ;
using System.Data ;
namespace CustomColumn
{
/// <summary>
/// DataGridCustomColumn 的摘要说明。
/// </summary>
public class DropDownListColumn :System.Web.UI.WebControls.DataGridColumn
{
//数据源
public DataTable DataSource ;
//显示的文本字段
public string DataTextField ;
//
public string DataValueField ;
//DataGrid中要绑定的字段
public string DataField ;
public string DataShowField;
public DropDownListColumn()
{
//
// TODO: 在此处加入构造函数逻辑
//
}
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
{
base.InitializeCell (cell, columnIndex, itemType);
switch(itemType)
{
case ListItemType.Header :
cell.Text = this.HeaderText ;
break;
case ListItemType.Item:case ListItemType.AlternatingItem :
cell.DataBinding +=new EventHandler(cell_ItemDataBinding);
break;
case ListItemType.EditItem :
cell.DataBinding +=new EventHandler(cell_EditItemDataBinding);
//========================
DropDownList drp = new DropDownList();
cell.Controls.Add(drp);
break;
}
}
private void cell_ItemDataBinding(object sender, EventArgs e)
{
TableCell cell =(TableCell)sender ;
DataGridItem DGI =(DataGridItem)cell.NamingContainer ;
try
{
cell.Text = (DataBinder.Eval(DGI.DataItem,DataShowField)).ToString();
}
catch
{
throw new Exception("请设置字段");
}
}
private void cell_EditItemDataBinding(object sender,EventArgs e)
{
TableCell cell =(TableCell)sender ;
DropDownList drp =(DropDownList)cell.Controls[0]; ;
ListItem item ;
DataGridItem DGI ;
try
{
//================
drp.Items.Add(new ListItem("请选择","-1"));
//
for(int k=0;k<=DataSource.Rows.Count -1;k++)
{
DataRow dr = DataSource.Rows[k];
item = new ListItem();
item.Text = dr[DataTextField].ToString();
item.Value = dr[DataValueField].ToString();
drp.Items.Add(item);
}
DGI =(DataGridItem)cell.NamingContainer ;
string Value = DataBinder.Eval(DGI.DataItem,DataField).ToString();
item = drp.Items.FindByValue(Value);
if(item!=null)
{
item.Selected = true;
}
}
catch
{
throw new Exception("数据源不正确。");
}
}
}
}
为DataGrid 写一个 DropDownListColumn的更多相关文章
- 怎样写一个webpack loader
div{display:table-cell;vertical-align:middle}#crayon-theme-info .content *{float:left}#crayon-theme- ...
- 请写一个php函数,可以接受任意数量的参数
请写一个php函数,可以接受任意数量的参数 这是一道面试题.怎么写这个函数呢? function fun(......) { } ----------------------------------- ...
- 学记:为spring boot写一个自动配置
spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...
- 自己写一个 jQuery 插件
我知道这一天终将会到来,现在,它来了. 需求 开发 SharePoint 的 CSOM 应用时,经常需要在网页上输出一些信息. 这种需求和 alert 的弹窗.F12 的断点查看信息的场景是不一样的: ...
- 深入浅出React Native 3: 从零开始写一个Hello World
这是深入浅出React Native的第三篇文章. 1. 环境配置 2. 我的第一个应用 将index.ios.js中的代码全部删掉,为什么要删掉呢?因为我们准备从零开始写一个应用~学习技术最好的方式 ...
- 【转】用C写一个简单病毒
[摘要]在分析病毒机理的基础上,用C语言写了一个小病毒作为实例,用TURBOC2.0实现. [Abstract] This paper introduce the charateristic of t ...
- JS入门学习,写一个时钟~
<!-- 耽搁了几天,于是又继续回到JS的学习了~~ 各种头大,加油吧... --> <!doctype html><html><head> <t ...
- JS高级群的日常!写一个从10到0的倒计时,用console.log打印,不可以用 setInterval!本来说好的研究avalonJS最后演变成了看着大神在那边互相比拼实力。。
JS高级群的日常!写一个从10到0的倒计时,用console.log打印,不可以用 setInterval!本来说好的研究avalonJS最后演变成了看着大神在那边互相比拼实力.. 小森执行一 ...
- ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 112[ ...
随机推荐
- ch1 About thinking skills
When confronted with a problem , we think about it. The issue, of course, is that our efforts may be ...
- ubuntu系统中查看本机cpu和内存信息的命令和用法
https://zhidao.baidu.com/question/192966322.html 写出ubuntu linux系统中查看本机cpu和内存信息的命令和用法,以及如何解读这些命令 ubun ...
- 【C++】智能指针简述(三):scoped_ptr
在介绍scoped_ptr之前,我们先回顾一下前两篇文章的内容. 首先,智能指针采用RAII机制,通过对象来管理指针,构造对象时,完成资源的初始化;析构对象时,对资源进行清理及汕尾. auto_ptr ...
- vc++中 .H 头文件引用的顺序与符号关系
在使用 #include "math.h" 和 #include <math.h>时,引号 与尖括号的区别如下 此时math.h_1 在工程文件中 math.h_2 ...
- union与union all
union在合并A和B表的时候会先将B表中的数据与A表进行比较,若A表中已存在,则忽略. union all合并时则不比较,直接将A表与B表进行合并. 因此,若已知A与B不存在重复数据,那么union ...
- enote笔记法的思考
章节:enote笔记法的思考 why enote笔记法: key1)大脑喜欢颜色. 我们的大脑天生就喜欢颜色.对颜色很敏感,这是由我们人类过去的演化历程决定的. 你可以理解为,文字有了颜色,让这个 ...
- 字符串匹配「 KMP 算法 」
引言 众所周知,字符串无论是在 OI 中还是别的计算机领域都占有比较大的比重,今天说的就是一个关于匹配字符串的算法——「 KMP 算法 」. 0x00 KMP 算法用于解决这样的一类问题:给定一个文本 ...
- Libreswan软件的密钥协商协议IKEv1主模式实现分析
Libreswan软件的密钥协商协议IKEv1主模式实现分析 1 协商过程 IKEv1(互联网密钥交换协议第一版)是IPsec VPN隧道协商使用的标准密钥协商协议,其协商过程如下图所示. 总共来回交 ...
- 利用stylist插件,简单两步屏蔽新浪微博上的广告
以前新浪微博只是在侧栏有几块小小的广告,还算可以接受,想着忍忍就算了,可最近真是越来越不厚道了,自从和淘宝合作之后,侧栏就开始有一大块广告根据你在淘宝的搜索记录推荐商品,更可恶的是信息流里的祛痘微博现 ...
- BZOJ 1666 USACO 2006 Oct. 奶牛的数字游戏
直接模拟2333 #include<cstdio> #include<algorithm> using namespace std; int n,ans; void read( ...