Free Programming

我的生活在一行又一行的代码中前行........

 

DataGrid操作小技巧

DataGrid将布尔值转换为“是”与“否”
<%# IIf(DataBinder.Eval(Container,"DataItem.sex"),"男","女") %>
上面是vb的写法c#的可以参照:http://www.chinaaspx.com/comm/dotnetbbs/5/70001.htm

改变datagrid中行颜色

Private Sub dgrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgrid.ItemDataBound

If Not e.Item.ItemIndex = -1 Then
e.Item.Attributes.Add("onmouseover", "this.setAttribute('BKC',this.style.backgroundColor);this.style.backgroundColor='#EEEEEE'")
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=this.getAttribute('BKC');")
End If

Dim FolderID As String = Request.QueryString("FolderID")
End Sub
删除确认:
<asp:ButtonColumn Text="<div onclick="return confirm('确认要删除吗?')"><img src=../../images/shared/Delete.gif></div>" HeaderText="删除" CommandName="Delete"> <HeaderStyle Wrap="False" HorizontalAlign="Center"></HeaderStyle> <ItemStyle Wrap="False" Width="35" HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>

在DataGrid新增一序号列,即类似Excel中的行号
添加模板列
<Columns>
<asp:TemplateColumn HeaderText="" ItemStyle-Width="50">
<ItemTemplate>
<%# Container.ItemIndex+1 %>
</ItemTemplate>
</asp:TemplateColumn>
</column>

实现datagrid 多列求和
Sub DataGrid_DataBound(obj as object,e as DataGridItemEventArgs)
Dim intTotal as integer
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
Dim lbl_id as label=e.item.findControl("lbl_id")
intTotal+=cDbl(lbl_id.text)
Case listItemType.footer
e.item.cells(0).text="总计:" & intTotal.tostring
e.item.cells(0).Attributes.add("Align","center")
End Select
End Sub
一。其中e.item.findControl("lbl_id")的lbl_id是dataGrid中一行的一个文本框或标签;
二。加在DataGrid中的OnItemDataBound="DataGrid_DataBound"事件中;

datagrid中根据不同的值显示不同的颜色

Private Sub grdcheckequipment_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdcheckequipment.ItemDataBound
' 确保处理的是数据行,而不是Header或者Footer
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
' 得到Manager字段的值
Try
Dim isManager As DateTime = CType(DataBinder.Eval(e.Item.DataItem, "首次接入时间"), DateTime)
If isManager = "1900-01-01" Then
e.Item.Cells(6).ForeColor =color.red
End If
Catch
End Try


End If
End Sub

单击DataGrid的行,使这行的数据显示在这个页面的下面,有点像winform的一个列表,下面一个明细!!!^_^^_^^_^

用处:可以使一个页面显示更多的内容

*.cs文件
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","this.style['cursor']='hand'");
e.Item.Attributes.Add("onclick","javascript:itemclick('"+e.Item.Cells[2].Text+"','"+((Label)e.Item.FindControl("lblTime")).Text+"')");
}

*.aspx文件
function itemclick(time,obj)
{
document.form1.all("label1").innerText = time;
 document.form1.txt.value = obj;
}

<DIV id="label1" runat="server">统计时间</DIV>
<INPUT id="txt" type="text" runat="server" value=入库时间>

<asp:BoundColumn DataField="time1" HeaderText="统计时间">
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="入库时间">
<ItemTemplate>
<asp:Label ID=“lblTime” Runat=server Text='<%# DataBinder.Eval(Container.DataItem,"rukushijian")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

数据的绑定DataBinder

学习了一段时间.net后,发掘.net性能对于一个application至关重要,所以现在经常找一些可以提高性能的技巧,下面是有关DataBinder的一个小技巧。

对于一般的绑定,我们使用<%# DataBinder.Eval(Container.DataItem, "字段名") %>

用DataBinder.eval 绑定不必关心数据来源。不必关心数据的类型eval会把这个数据对象转换为一个字符串。在底层绑定做了很多工作,使用了反射性能。正因为使用方便了,但却影响了数据性能.
来看下<%# DataBinder.Eval(Container.DataItem, "字段名") %>。当于dataset绑定时DataItem其实是一个DataRowView(如果绑定的是一个数据读取器datareader)它就是一个IdataRecord。)因此直接转换成DataRowView的话,将会给性能带来很大提升 <%# ctype(Container.DataItem,DataRowView).Row("字段名") %> 对数据的绑定建议使用<%# ctype(Container.DataItem,DataRowView).Row("字段名") %>。数据量大的时候可提高几百倍的速度。使用时注意2方面:1.需在页面添加<%@ Import namespace="System.Data"%>.2.注意字段名的大小写(要特别注意)。如果和查询的不一致,在某些情况下会导致比<%# DataBinder.Eval(Container.DataItem, "字段名") %>还要慢。如果想进一步提高速度,可采用<%# ctype(Container.DataItem,DataRowView).Row(0) %>的方法。不过其可读性不高

posted on 2007-04-25 12:26  sharewind  阅读(372)  评论(0编辑  收藏  举报

导航