using system;
using system.collections;
using system.data;
using system.web.ui.webcontrols;
namespace my.utils {
/// <summary>
/// datagrid工具集
/// </summary>
public class datagridutils {
public datagridutils() {
}
/// <summary>
/// 返回datagrid的模板列的checkbox
/// </summary>
/// <param name=”dglist”>datagrid</param>
/// <returns>选中记录的值</returns>
public static string[] getid (datagrid dglist) {
return getid(dglist, “chkid”);
}
/// <summary>
/// 返回datagrid的模板列的checkbox
/// </summary>
/// <param name=”dglist”>datagrid</param>
/// <param name=”chkid”>checkbox的服务器端id</param>
/// <returns>选中记录的值</returns>
public static string[] getid (datagrid dglist, string chkid) {
return getid(dglist, chkid, 0);
}
/// <summary>
/// 返回datagrid的模板列的checkbox
/// </summary>
/// <param name=”dglist”>datagrid</param>
/// <param name=”chkid”>checkbox的服务器端id</param>
/// <param name=”index”>选中的值的索引列</param>
/// <returns>选中记录的值</returns>
public static string[] getid (datagrid dglist, string chkid, int index) {
ilist list = new arraylist();
foreach(datagriditem dgi in dglist.items) {
//chkid是每个记录项的id,记住,这是服务器端的id,不是客户端的id.
system.web.ui.control l_ct = dgi.cells[0].findcontrol(chkid);
if(l_ct != null) {
//下面语句是判断是否记录项的checkbox框初选种
if(((system.web.ui.webcontrols.checkbox)l_ct).checked) {
list.add(dgi.cells[index].text);
}
}
}
if (list.count == 0) {
return null;
} else {
string[] s = new string[list.count];
list.copyto(s, 0);
return s;
}
}
/// <summary>
/// 设置datagrid的分页
/// </summary>
/// <param name=”dg”>datagrid</param>
/// <param name=”dt”>datatable</param>
public static void setpage (datagrid dg, datatable dt) {
//最大页
int maxpage = (int)(dt.rows.count + dg.pagesize – 1) / dg.pagesize;
if (maxpage == 0) {
maxpage = 1;
}
if (dg.currentpageindex >= maxpage){
dg.currentpageindex = maxpage – 1;
}
//如果是datagridtable,设置全部记录数,polluxliu.common.datagridtable只是为了兼容crm之前的项目
if (dg.gettype().fullname.equals(“polluxliu.common.datagridtable”)) {
//((polluxliu.common.datagridtable)dg).maxrecord = dt.rows.count;
} else if (dg.gettype().fullname.equals(“polluxliu.ui.webcontrols.datagridtable”)) {
((polluxliu.ui.webcontrols.datagridtable)dg).maxrecord = dt.rows.count;
}
dg.datasource = dt;
}
}
}