datagrid使用心得(附大量代码)
1. 为 datagrid 控件设计样式
在<asp:datagrid id="datagrid1" runat="server">之后添加如下代码
<footerstyle forecolor="black" backcolor="#cccccc"></footerstyle>
<selecteditemstyle font-bold="true" forecolor="white" backcolor="#008a8c"></selecteditemstyle>
<alternatingitemstyle backcolor="gainsboro"></alternatingitemstyle>
<itemstyle forecolor="black" backcolor="#eeeeee"></itemstyle>
<headerstyle font-bold="true" forecolor="white" backcolor="#000084"></headerstyle>
说明:
(1) 在每个标签内主要是 forecolor backcolor font-bold 这几个属性值
2. 为 datagrid 控件添加绑定列
<asp:boundcolumn datafield="" readonly="true" headertext=""></asp:boundcolumn>
说明:
(1) 在标签内的基本属性是 datafield / headertext
(2) dataformatstring 用于 获取或设置指定列中各项的显示格式的字符串。
形式为 { a: bxx }。例如,格式化字符串 {0:f2} 将显示带两位小数的定点数。
其中a值只能设置为 0,因为每个单元格中只有一个值。
冒号后的字符(常规示例中为 b)指定值的显示格式
c 以货币格式显示数值。
d 以十进制格式显示数值。
e 以科学记数法(指数)格式显示数值。
f 以固定格式显示数值。
g 以常规格式显示数值。
n 以数字格式显示数值。
x 以十六进制格式显示数值。
(3) visible 获取或设置一个值,该值指示此列在 datagrid 控件中是否可见。
(4) readonly 设置些列是否只读,若是只读的话,则不能修改.
(5) sortexpression 获取或设置选择进行排序的列时传递到 onsortcommand 方法的字段或表达式的名称。
3. 为 datagrid 控件添加模板列
<asp:templatecolumn headertext="类别">
<itemtemplate>
<asp:label text=<%# databinder.eval(container.dataitem, "actorclassname") %> runat="server" id="label1"/>
</itemtemplate>
<edititemtemplate>
<select name="sltclassname">
<% = actorclass.getparentclass(0) %>
</select>
</edititemtemplate>
</asp:templatecolumn>
说明:
(1) 基本框架是
<asp:templatecolumn headertext="类别">
<itemtemplate></itemtemplate>
</asp:templatecolumn>
(2) 全面的模板列
<asp:templatecolumn>
<headertemplate>
<b> tax </b>
</headertemplate>
<itemtemplate>
<asp:label
text=<%# databinder.eval(container.dataitem, "tax") %>
runat="server"/>
</itemtemplate>
<edititemtemplate>
<asp:checkbox
text="taxable"
runat="server"/>
</edititemtemplate>
<footertemplate>
<asp:hyperlink id="hyperlink1"
text="microsoft"
navigateurl="http://www.microsoft.com"
runat="server"/>
</footertemplate>
</asp:templatecolumn>
(3) 为布尔型列应用模板列
<asp:templatecolumn>
<itemtemplate>
<asp:label
text=<%# databinder.eval(container.dataitem, "tax") %>
runat="server"/>
</itemtemplate>
<edititemtemplate>
<asp:checkbox
text="taxable"
runat="server"/>
</edititemtemplate>
</asp:templatecolumn>
在正常状态,用 label控件显示
在编辑状态,用 checkbox控件显示
(4) 为枚举类型列应用模板列,如业务地区(全网/广东/云南等等)
<asp:templatecolumn headertext="处理方式">
<itemtemplate>
<asp:label id="lbstatus">
<%# databinder.eval(container, "dataitem.dealwith") %>
</asp:label>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist id="dpstatus2" runat="server" datatextfield="status">
<asp:listitem value="log">log(日志)</asp:listitem>
<asp:listitem value="sendsms">sendsms(短信)</asp:listitem>
</asp:dropdownlist>
</edititemtemplate>
</asp:templatecolumn>
在正常状态,用 label控件显示
在编辑状态,用 dropdownlist控件显示
(5) 为长字符串应用模板列,如一篇文章的内容
还未做过
4. 为 datagrid 控件添加按钮列
<asp:buttoncolumn
headertext="remove from cart"
buttontype="pushbutton"
text="remove"
commandname="removefromcart" />
(1) 要使用按钮列,必须在 datagrid 控件中添加 onitemcommand 属性,并为该事件添加处理方法.
(2) 模板列可以实现按钮列能实现的任何功能.
5. 为 datagrid 控件添加编辑列
<asp:editcommandcolumn buttontype="linkbutton" updatetext="更新" headertext="编辑" canceltext="取消" edittext="编辑"></asp:editcommandcolumn>
(1) buttontype 有两个值: linkbutton 超级链接样式按钮的列 | pushbutton 普通按钮的列。
6. 为 datagrid 控件添加超链接列
<asp:hyperlinkcolumn text="添加子类" datanavigateurlfield="actorclassid" datanavigateurlformatstring="addactorclass.aspx?classid={0}"></asp:hyperlinkcolumn>
(1) 为每一行设置相同的文字及跳转的url地址
设置 text 和 navigateurl 属性,则列中的所有超级链接将共享同一标题和 url
(2) 为每一行设置不同的文字及不同跳转的url地址
a. 用 datatextfield 设置数据源字段,若还想在原数据的基础上加工一下(如字段值为300,想显示为300元)
则再设置 datatextformatstring 字段
b. 用datanavigateurlfield 及 datanavigateurlformatstring 来设置url地址
用datatextfield = "money" datatextformatstring = "{0}元"
c. 举例
datanavigateurlfield="actorclassid" datanavigateurlformatstring="addactorclass.aspx?classid={0}"
7. 为 datagrid 控件添加"编辑"代码
在 datagrid 标签中加入
onupdatecommand="datagrid1_update" oncancelcommand="datagrid1_cancel" oneditcommand="datagrid1_edit"代码
在codebehind页面加入如下代码
///响应编辑按钮
public void datagrid1_edit(object sender, datagridcommandeventargs e)
{
datagrid1.edititemindex = e.item.itemindex;
if (request.querystring.get("classid") != null)
common.binddata(datagrid1,common.getsource("select * from actorclass where parentid=" + request.querystring.get("classid") + " order by depth,orderid desc"));
else
common.binddata(datagrid1,common.getsource("select * from actorclass where depth=1 order by depth,orderid desc"));
}
///响应取消按钮
public void datagrid1_cancel(object sender, datagridcommandeventargs e)
{
datagrid1.edititemindex = -1;
if (request.querystring.get("classid") != null)
common.binddata(datagrid1,common.getsource("select * from actorclass where parentid=" + request.querystring.get("classid") + " order by depth,orderid desc"));
else
common.binddata(datagrid1,common.getsource("select * from actorclass where depth=1 order by depth,orderid desc"));
}
///响应更新按钮
public void datagrid1_update(object sender, datagridcommandeventargs e)
{
textbox classnametext = (textbox)e.item.cells[1].controls[0];
string classname = classnametext.text;
int classid = int32.parse((e.item.cells[0].text).tostring());
textbox orderid2 = (textbox)e.item.cells[5].controls[0];
int orderid = int32.parse(orderid2.text);
actorclass.modifyactorclass(classname,classid,orderid);
datagrid1.edititemindex = -1;
if (request.querystring.get("classid") != null)
common.binddata(datagrid1,common.getsource("select * from actorclass where parentid=" + request.querystring.get("classid") + " order by depth,orderid desc"));
else
common.binddata(datagrid1,common.getsource("select * from actorclass where depth=1 order by depth,orderid desc"));
}
说明
(1) datagrid 事件处理程序的格式
methodname(object sender, datagridcommandeventargs e)
(2) 更新按钮的说明
a. 获取编辑状态中的文本框
textbox classnametext = (textbox)e.item.cells[1].controls[0];
string classname = classnametext.text;
b. 获取编辑状态中的下拉列表框
方法一
int classid;
classid = int32.parse(request.form.get("sltclassname"));
方法二
dropdownlist bbb = (dropdownlist)e.item.cells[10].findcontrol("dpstatus2");
string ddpvalue = bbb.selectedvalue
c. 获取编辑状态中的复选框
bool boolenabled = ((checkbox)e.item.findcontrol("chk_enabled")).checked;
string str2;
if (boolenabled)
{
str2="1";
}
else
{
str2="0";
}
赋值给 str2 ,原因是插入到数据库的布尔型值只能是 1 或者 0
d. 获取编辑状态中的文本值,即该列是只读的.
string storyid = (e.item.cells[0].text).tostring();
8. 为 datagrid 控件添加分页事件
在 datagrid 控件标签中加入如下代码
onpageindexchanged="datagrid1_pageindexchanged"
在后台中加入如下代码
/// <summary>
/// 响应分页事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void datagrid1_page(object sender, datagridpagechangedeventargs e)
{
datagrid1.currentpageindex = e.newpageindex;
databind();
}
9. 为 datagrid 控件添加绑定事件,即在 datagrid绑定时发生的事件处理
一般用些事件来,做一些页面上的效果.如更改背景色,文本框大小等.
onitemdatabound="datagrid1_itemdatabound"
/// <summary>
/// 响应datagrid绑定事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void datagrid1_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
{
if (e.item.itemtype == listitemtype.item)
{
e.item.attributes.add("onmouseover","this.style.backgroundcolor=#c8dafa");
e.item.attributes.add("onmouseout","this.style.backgroundcolor=white");
}
else if (e.item.itemtype == listitemtype.alternatingitem)
{
e.item.attributes.add("onmouseover","this.style.backgroundcolor=#c8dafa");
e.item.attributes.add("onmouseout","this.style.backgroundcolor=#f6f6f6");
}
}
10. 为 datagrid 控件添加接钮处理事件程序
在 datagrid 控件标签中加入如下代码
onitemcommand="itemsgrid_command"
在后台中加入如下代码
public void itemsgrid_command(object sender, datagridcommandeventargs e)
{
switch(((linkbutton)e.commandsource).commandname)
{
case "delete":
int classid = int32.parse((e.item.cells[0].text).tostring());
actorclass.deleteactorclass(classid);
if (request.querystring.get("classid") != null)
common.binddata(datagrid1,common.getsource("select * from actorclass where parentid=" + request.querystring.get("classid") + " order by depth,orderid desc"));
else
common.binddata(datagrid1,common.getsource("select * from actorclass where depth=1 order by depth,orderid desc"));
break;
// add other cases here, if there are multiple buttoncolumns in
// the datagrid control.
case "hidden":
int actorid = int32.parse((e.item.cells[0].text).tostring());
actorclass.hiddenactorclass(actorid);
if (request.querystring.get("classid") != null)
common.binddata(datagrid1,common.getsource("select * from actorclass where parentid=" + request.querystring.get("classid") + " order by depth,orderid desc"));
else
common.binddata(datagrid1,common.getsource("select * from actorclass where depth=1 order by depth,orderid desc"));
break;
case "moveup":
int actorclassid = int32.parse((e.item.cells[0].text).tostring());
string orderid = (e.item.cells[5].text).tostring();
actorclass.moveup(orderid,actorclassid);
if (request.querystring.get("classid") != null)
common.binddata(datagrid1,common.getsource("select * from actorclass where parentid=" + request.querystring.get("classid") + " order by depth,orderid desc"));
else
common.binddata(datagrid1,common.getsource("select * from actorclass where depth=1 order by depth,orderid desc"));
break;
case "movedown":
actorclassid = int32.parse((e.item.cells[0].text).tostring());
orderid = (e.item.cells[5].text).tostring();
actorclass.movedown(orderid,actorclassid);
if (request.querystring.get("classid") != null)
common.binddata(datagrid1,common.getsource("select * from actorclass where parentid=" + request.querystring.get("classid") + " order by depth,orderid desc"));
else
common.binddata(datagrid1,common.getsource("select * from actorclass where depth=1 order by orderid"));
break;
default:
// do nothing.
break;
}
}
11. 为 datagrid添加模板列,但是内容根据字段值来显示"链接",还是文本
以下三个都是,根据字段列不同,而显示内容及显示控件不同的处理代码.
<asp:templatecolumn headertext="子菜单">
<itemtemplate>
<%# actorclassmanage.hasleaf(databinder.eval(container.dataitem,"actorclassid").tostring(),databinder.eval(container.dataitem,"child").tostring())%>
</itemtemplate>
</asp:templatecolumn>
public static string hasleaf(string id,string child)
{
string lrtn = "";
if (int32.parse(child) > 0)
lrtn="<a href=actorclassmanage.aspx?classid="+id+"><font color=blue>子菜单(" + child + ")</font></a>";
else
lrtn = "无子菜单";
return lrtn;
}
<asp:templatecolumn headertext="属性">
<itemtemplate>
<asp:linkbutton text=<%# ishidden(databinder.eval(container.dataitem,"actorclassid").tostring(),(bool)databinder.eval(container.dataitem,"enabled")) %> runat="server" commandname="hidden" id="linkbutton1"></asp:linkbutton>
</itemtemplate>
</asp:templatecolumn>
public static string ishidden(string id,bool enabled)
{
string lrtn = "";
if (enabled == true)
{
lrtn = "[显示]";
}
else
{
lrtn = "隐藏";
}
return lrtn;
}
public static void sort(string actorclassid, string orderid)
{
string temp = "";
if (int32.parse(bgpicmanage.getmaxcode("actorclass","orderid")) == int32.parse(orderid))
{
temp += "<ipnut type=submit value=向下移>";
}
if (int32.parse(orderid) == 0)
{
temp += "<ipnut type=submit value=向上移>";
}
}
12. datagrid 控件自定义分页代码
将下列代码放于包含<datagrid>的form中去,
<p style="font-size:9pt" align="center">
<asp:label id="lblpagecount" runat="server"></asp:label>
<asp:label id="lblcurrentindex" runat="server"></asp:label>
<asp:linkbutton id="btnfirst" οnclick="pagerbuttonclick" runat="server" font-name="verdana"
font-size="8pt" forecolor="navy" commandargument="0"></asp:linkbutton>
<asp:linkbutton id="btnprev" οnclick="pagerbuttonclick" runat="server" font-name="verdana"
font-size="8pt" forecolor="navy" commandargument="prev"></asp:linkbutton>
<asp:linkbutton id="btnnext" οnclick="pagerbuttonclick" runat="server" font-name="verdana"
font-size="8pt" forecolor="navy" commandargument="next"></asp:linkbutton>
<asp:linkbutton id="btnlast" οnclick="pagerbuttonclick" runat="server" font-name="verdana"
font-size="8pt" forecolor="navy" commandargument="last"></asp:linkbutton>
</p>
后台代码
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
btnfirst.text = "最首页";
btnprev.text = "前一页";
btnnext.text = "下一页";
btnlast.text = "最后页";
//绑定数据源
if (!page.ispostback)
{
opendatabase();
bindgrid();
}
}
//用于显示"第几页,总*页"
private void showstats()
{
lblcurrentindex.text = "第 " + (mydatagrid.currentpageindex + 1).tostring() + " 页";
lblpagecount.text = "总共 " + mydatagrid.pagecount.tostring() + " 页";
}
//响应分页按钮
public void pagerbuttonclick(object sender, eventargs e)
{
string arg = ((linkbutton)sender).commandargument.tostring();
switch(arg)
{
case "next":
if (mydatagrid.currentpageindex < (mydatagrid.pagecount - 1))
{
mydatagrid.currentpageindex += 1;
}
break;
case "prev":
if (mydatagrid.currentpageindex > 0)
{
mydatagrid.currentpageindex -= 1;
}
break;
case "last":
mydatagrid.currentpageindex = (mydatagrid.pagecount - 1);
break;
default:
mydatagrid.currentpageindex = system.convert.toint32(arg);
break;
}
bindgrid();
showstats();
}