电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

DataGrid使用心得(附大量代码)


发布日期:2021/10/21
 

为DataGrid控件设计样式

在<asp:datagridid=DataGridrunat=server>之后添加如下代码

<FooterStyleForeColor=BlackBackColor=#CCCCCC></FooterStyle>

<SelectedItemStyleFontBold=TrueForeColor=WhiteBackColor=#AC></SelectedItemStyle>

<AlternatingItemStyleBackColor=Gainsboro></AlternatingItemStyle>

<ItemStyleForeColor=BlackBackColor=#EEEEEE></ItemStyle>

<HeaderStyleFontBold=TrueForeColor=WhiteBackColor=#></HeaderStyle>

说明:

()在每个标签内主要是ForeColorBackColorFontBold这几个属性值

为DataGrid控件添加绑定列

<asp:BoundColumnDataField=ReadOnly=TrueHeaderText=></asp:BoundColumn>

说明:

()在标签内的基本属性是DataField/HeaderText

()DataFormatString用于获取或设置指定列中各项的显示格式的字符串

形式为{A:Bxx}例如格式化字符串{:F}将显示带两位小数的定点数

其中A值只能设置为因为每个单元格中只有一个值

冒号后的字符(常规示例中为B)指定值的显示格式

C以货币格式显示数值

D以十进制格式显示数值

E以科学记数法(指数)格式显示数值

F以固定格式显示数值

G以常规格式显示数值

N以数字格式显示数值

X以十六进制格式显示数值

()Visible获取或设置一个值该值指示此列在DataGrid控件中是否可见

()ReadOnly设置些列是否只读若是只读的话则不能修改

()SortExpression获取或设置选择进行排序的列时传递到OnSortCommand方法的字段或表达式的名称

为DataGrid控件添加模板列

<asp:TemplateColumnHeaderText=类别>

<ItemTemplate>

<asp:LabelText=<%#DataBinderEval(ContainerDataItemactorclassname)%>runat=serverID=Label/>

</ItemTemplate>

<EditItemTemplate>

<selectname=sltclassname>

<%=ActorClassGetParentClass()%>

</select>

</EditItemTemplate>

</asp:TemplateColumn>

说明:

()基本框架是

<asp:TemplateColumnHeaderText=类别>

<ItemTemplate></ItemTemplate>

</asp:TemplateColumn>

()全面的模板列

<asp:TemplateColumn>

<HeaderTemplate>

<b>Tax</b>

</HeaderTemplate>

<ItemTemplate>

<asp:Label

Text=<%#DataBinderEval(ContainerDataItemTax)%>

runat=server/>

</ItemTemplate>

<EditItemTemplate>

<asp:CheckBox

Text=Taxable

runat=server/>

</EditItemTemplate>

<FooterTemplate>

<asp:HyperLinkid=HyperLink

Text=Microsoft

NavigateUrl=

runat=server/>

</FooterTemplate>

</asp:TemplateColumn>

()为布尔型列应用模板列

<asp:TemplateColumn>

<ItemTemplate>

<asp:Label

Text=<%#DataBinderEval(ContainerDataItemTax)%>

runat=server/>

</ItemTemplate>

<EditItemTemplate>

<asp:CheckBox

Text=Taxable

runat=server/>

</EditItemTemplate>

</asp:TemplateColumn>

在正常状态用Label控件显示

在编辑状态用CheckBox控件显示

()为枚举类型列应用模板列如业务地区(全网/广东/云南等等)

<asp:TemplateColumnHeaderText=处理方式>

<ItemTemplate>

<asp:LabelID=lbStatus>

<%#DataBinderEval(ContainerDataItemDealWith)%>

</asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:DropDownListid=dpStatusrunat=serverDataTextField=status>

<asp:ListItemValue=Log>Log(日志)</asp:ListItem>

<asp:ListItemValue=SendSms>SendSms(短信)</asp:ListItem>

</asp:DropDownList>

</EditItemTemplate>

</asp:TemplateColumn>

在正常状态用Label控件显示

在编辑状态用DropDownList控件显示

()为长字符串应用模板列如一篇文章的内容

还未做过

为DataGrid控件添加按钮列

<asp:ButtonColumn

HeaderText=Removefromcart

ButtonType=PushButton

Text=Remove

CommandName=RemoveFromCart/>

()要使用按钮列必须在DataGrid控件中添加OnItemCommand属性并为该事件添加处理方法

()模板列可以实现按钮列能实现的任何功能

为DataGrid控件添加编辑列

<asp:EditCommandColumnButtonType=LinkButtonUpdateText=更新HeaderText=编辑CancelText=取消EditText=编辑></asp:EditCommandColumn>

()ButtonType有两个值:LinkButton超级链接样式按钮的列|PushButton普通按钮的列

为DataGrid控件添加超链接列

<asp:HyperLinkColumnText=添加子类DataNavigateUrlField=ActorclassIDDataNavigateUrlFormatString=addActorClassaspx?classID={}></asp:HyperLinkColumn>

()为每一行设置相同的文字及跳转的URL地址

设置Text和NavigateUrl属性则列中的所有超级链接将共享同一标题和URL

()为每一行设置不同的文字及不同跳转的URL地址

A用DataTextField设置数据源字段若还想在原数据的基础上加工一下(如字段值为想显示为元)

则再设置DataTextFormatString字段

B用DataNavigateUrlField及DataNavigateUrlFormatString来设置URL地址

用DataTextField=moneyDataTextFormatString={}元

C举例

DataNavigateUrlField=ActorclassIDDataNavigateUrlFormatString=addActorClassaspx?classID={}

为DataGrid控件添加编辑代码

在DataGrid标签中加入

OnUpdateCommand=DataGrid_UpdateOnCancelCommand=DataGrid_CancelOnEditCommand=DataGrid_Edit代码

在codeBehind页面加入如下代码

///响应编辑按钮

publicvoidDataGrid_Edit(ObjectsenderDataGridCommandEventArgse)

{

DataGridEditItemIndex=eItemItemIndex;

if(RequestQueryStringGet(classID)!=null)

CommonBindData(DataGridCommonGetSource(select*fromActorClasswhereparentID=+RequestQueryStringGet(classID)+orderbydepthorderIDdesc));

else

CommonBindData(DataGridCommonGetSource(select*fromActorClasswheredepth=orderbydepthorderIDdesc));

}

///响应取消按钮

publicvoidDataGrid_Cancel(ObjectsenderDataGridCommandEventArgse)

{

DataGridEditItemIndex=;

if(RequestQueryStringGet(classID)!=null)

CommonBindData(DataGridCommonGetSource(select*fromActorClasswhereparentID=+RequestQueryStringGet(classID)+orderbydepthorderIDdesc));

else

CommonBindData(DataGridCommonGetSource(select*fromActorClasswheredepth=orderbydepthorderIDdesc));

}

///响应更新按钮

publicvoidDataGrid_Update(ObjectsenderDataGridCommandEventArgse)

{

TextBoxClassNameText=(TextBox)eItemCells[]Controls[];

stringclassName=ClassNameTextText;

intclassID=IntParse((eItemCells[]Text)ToString());

TextBoxorderID=(TextBox)eItemCells[]Controls[];

intorderID=IntParse(orderIDText);

ActorClassModifyActorClass(classNameclassIDorderID);

DataGridEditItemIndex=;

if(RequestQueryStringGet(classID)!=null)

CommonBindData(DataGridCommonGetSource(select*fromActorClasswhereparentID=+RequestQueryStringGet(classID)+orderbydepthorderIDdesc));

else

CommonBindData(DataGridCommonGetSource(select*fromActorClasswheredepth=orderbydepthorderIDdesc));

}

说明

()DataGrid事件处理程序的格式

MethodName(ObjectsenderDataGridCommandEventArgse)

()更新按钮的说明

A获取编辑状态中的文本框

TextBoxClassNameText=(TextBox)eItemCells[]Controls[];

stringclassName=ClassNameTextText;

B获取编辑状态中的下拉列表框

方法一

intclassID;

classID=IntParse(RequestFormGet(sltclassname));

方法二

DropDownListbbb=(DropDownList)eItemCells[]FindControl(dpStatus);

stringddpValue=bbbSelectedValue

C获取编辑状态中的复选框

boolboolEnabled=((CheckBox)eItemFindControl(chk_enabled))Checked;

Stringstr;

if(boolEnabled)

{

str=;

}

else

{

str=;

}

赋值给str原因是插入到数据库的布尔型值只能是或者

D获取编辑状态中的文本值即该列是只读的

stringstoryID=(eItemCells[]Text)ToString();

为DataGrid控件添加分页事件

在DataGrid控件标签中加入如下代码

OnPageIndexChanged=DataGrid_PageIndexChanged

在后台中加入如下代码

///<summary>

///响应分页事件

///</summary>

///<paramname=sender></param>

///<paramname=e></param>

publicvoidDataGrid_Page(ObjectsenderDataGridPageChangedEventArgse)

{

DataGridCurrentPageIndex=eNewPageIndex;

DataBind();

}

为DataGrid控件添加绑定事件即在DataGrid绑定时发生的事件处理

一般用些事件来做一些页面上的效果如更改背景色文本框大小等

OnItemDataBound=DataGrid_ItemDataBound

///<summary>

///响应DataGrid绑定事件

///</summary>

///<paramname=sender></param>

///<paramname=e></param>

publicvoidDataGrid_ItemDataBound(objectsenderSystemWebUIWebControlsDataGridItemEventArgse)

{

if(eItemItemType==ListItemTypeItem)

{

eItemAttributesAdd(onmouseoverthisstylebackgroundColor=#cdafa);

eItemAttributesAdd(onmouseoutthisstylebackgroundColor=white);

}

elseif(eItemItemType==ListItemTypeAlternatingItem)

{

eItemAttributesAdd(onmouseoverthisstylebackgroundColor=#cdafa);

eItemAttributesAdd(onmouseoutthisstylebackgroundColor=#fff);

}

}

为DataGrid控件添加接钮处理事件程序

在DataGrid控件标签中加入如下代码

OnItemCommand=ItemsGrid_Command

在后台中加入如下代码

publicvoidItemsGrid_Command(ObjectsenderDataGridCommandEventArgse)

{

switch(((LinkButton)eCommandSource)CommandName)

{

caseDelete:

intclassID=IntParse((eItemCells[]Text)ToString());

ActorClassDeleteActorClass(classID);

if(RequestQueryStringGet(classID)!=null)

CommonBindData(DataGridCommonGetSource(select*fromActorClasswhereparentID=+RequestQueryStringGet(classID)+orderbydepthorderIDdesc));

else

CommonBindData(DataGridCommonGetSource(select*fromActorClasswheredepth=orderbydepthorderIDdesc));

break;

//AddothercaseshereiftherearemultipleButtonColumnsin

//theDataGridcontrol

casehidden:

intactorID=IntParse((eItemCells[]Text)ToString());

ActorClassHiddenActorClass(actorID);

if(RequestQueryStringGet(classID)!=null)

CommonBindData(DataGridCommonGetSource(select*fromActorClasswhereparentID=+RequestQueryStringGet(classID)+orderbydepthorderIDdesc));

else

CommonBindData(DataGridCommonGetSource(select*fromActorClasswheredepth=orderbydepthorderIDdesc));

break;

caseMoveUp:

intactorclassID=IntParse((eItemCells[]Text)ToString());

stringorderID=(eItemCells[]Text)ToString();

ActorClassMoveUp(orderIDactorclassID);

if(RequestQueryStringGet(classID)!=null)

CommonBindData(DataGridCommonGetSource(select*fromActorClasswhereparentID=+RequestQueryStringGet(classID)+orderbydepthorderIDdesc));

else

CommonBindData(DataGridCommonGetSource(select*fromActorClasswheredepth=orderbydepthorderIDdesc));

break;

caseMoveDown:

actorclassID=IntParse((eItemCells[]Text)ToString());

orderID=(eItemCells[]Text)ToString();

ActorClassMoveDown(orderIDactorclassID);

if(RequestQueryStringGet(classID)!=null)

CommonBindData(DataGridCommonGetSource(select*fromActorClasswhereparentID=+RequestQueryStringGet(classID)+orderbydepthorderIDdesc));

else

CommonBindData(DataGridCommonGetSource(select*fromActorClasswheredepth=orderbyorderID));

break;

default:

//Donothing

break;

}

}

为DataGrid添加模板列但是内容根据字段值来显示链接还是文本

以下三个都是根据字段列不同而显示内容及显示控件不同的处理代码

<asp:TemplateColumnHeaderText=子菜单>

<ItemTemplate>

<%#ActorClassManagehasLeaf(DataBinderEval(ContainerDataItemActorClassID)ToString()DataBinderEval(ContainerDataItemchild)ToString())%>

</ItemTemplate>

</asp:TemplateColumn>

publicstaticstringhasLeaf(stringidstringchild)

{

stringlRtn=;

if(IntParse(child)>)

lRtn=<a+id+><fontcolor=blue>子菜单(+child+)</font></a>;

else

lRtn=无子菜单;

returnlRtn;

}

<asp:TemplateColumnHeaderText=属性>

<ItemTemplate>

<asp:LinkButtonText=<%#IsHidden(DataBinderEval(ContainerDataItemActorClassID)ToString()(bool)DataBinderEval(ContainerDataItemEnabled))%>runat=serverCommandName=hiddenID=Linkbutton></asp:LinkButton>

</ItemTemplate>

</asp:TemplateColumn>

publicstaticstringIsHidden(stringidboolenabled)

{

stringlRtn=;

if(enabled==true)

{

lRtn=[显示];

}

else

{

lRtn=隐藏;

}

returnlRtn;

}

publicstaticvoidSort(stringactorclassIDstringorderID)

{

stringtemp=;

if(IntParse(BgPicManageGetMaxCode(actorclassorderID))==IntParse(orderID))

{

temp+=<ipnuttype=submitvalue=向下移>;

}

if(IntParse(orderID)==)

{

temp+=<ipnuttype=submitvalue=向上移>;

}

}

DataGrid控件自定义分页代码

将下列代码放于包含<DataGrid>的form中去

<palign=center>

<asp:labelid=lblPageCountrunat=server></asp:label>

<asp:labelid=lblCurrentIndexrunat=server></asp:label>

<asp:linkbuttonid=btnFirstonclick=PagerButtonClickrunat=serverFontName=verdana

Fontsize=ptForeColor=navyCommandArgument=></asp:linkbutton>

<asp:linkbuttonid=btnPrevonclick=PagerButtonClickrunat=serverFontName=verdana

Fontsize=ptForeColor=navyCommandArgument=prev></asp:linkbutton>

<asp:linkbuttonid=btnNextonclick=PagerButtonClickrunat=serverFontName=verdana

Fontsize=ptForeColor=navyCommandArgument=next></asp:linkbutton>

<asp:linkbuttonid=btnLastonclick=PagerButtonClickrunat=serverFontName=verdana

Fontsize=ptForeColor=navyCommandArgument=last></asp:linkbutton>

</p>

后台代码

privatevoidPage_Load(objectsenderSystemEventArgse)

{

//在此处放置用户代码以初始化页面

btnFirstText=最首页;

btnPrevText=前一页;

btnNextText=下一页;

btnLastText=最后页;

//绑定数据源

if(!PageIsPostBack)

{

OpenDatabase();

BindGrid();

}

}

//用于显示第几页总*页

privatevoidShowStats()

{

lblCurrentIndexText=+(MyDataGridCurrentPageIndex+)ToString()+;

lblPageCountText=总共+MyDataGridPageCountToString()+;

}

//响应分页按钮

publicvoidPagerButtonClick(objectsenderEventArgse)

{

stringarg=((LinkButton)sender)CommandArgumentToString();

switch(arg)

{

casenext:

if(MyDataGridCurrentPageIndex<(MyDataGridPageCount))

{

MyDataGridCurrentPageIndex+=;

}

break;

caseprev:

if(MyDataGridCurrentPageIndex>)

{

MyDataGridCurrentPageIndex=;

}

break;

caselast:

MyDataGridCurrentPageIndex=(MyDataGridPageCount);

break;

default:

MyDataGridCurrentPageIndex=SystemConvertToInt(arg);

break;

}

BindGrid();

ShowStats();

}

上一篇:WCF实现上传图片功能

下一篇:重绘的滚动条控制ListBox的滚动