DataGrid操作小技巧

梦里梦外; 2022-04-01 10:24 291阅读 0赞

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
删除确认:

" HeaderText="删除" CommandName="Delete">

在DataGrid新增一序号列,即类似Excel中的行号
添加模板列


<%# Container.ItemIndex+1 %>


实现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;
}

统计时间





数据的绑定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) %>的方法。不过其可读性不高

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MyEclipse快捷操作技巧

    1.如何在Myeclipse设置显示行号   在使用 MyEclipse 开发程序的时候,不显示行号是一件很烦人的事情,要想显示行号,其实很简单, 按住 【Ctrl