DataGrid的ItemDataBound事件

DATAGRID中,如果要对某些记录进行格式化或者修饰,用到itemdatabound事件比较方便,
比如,要显示某人的存款金额已经少于某个数额了,要用红色来显示等。 itemdatabound事件发生在
数据绑定到datagrid后,而其内容发送到客户端前。
比如
private void OnItemDataBound(object sender,
        System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
      if (e.Item.ItemType == ListItemType.Item ||  e.Item.ItemType == ListItemType.AlternatingItem)
       {
            Label lblBalance = (Label)e.Item.FindControl("dgLabel2");
           double dblBalance = Convert.ToDouble(lblBalance.Text);
           if(dblBalance < 0)
            e.Item.Cells[3].ForeColor = System.Drawing.Color.Red;
      
            Label lblCountry = (Label)e.Item.FindControl("dgLabel1");
             string strCountry = lblCountry.Text;

        if(strCountry == "USA" || strCountry == "Mexico" || strCountry == "Canada")
          {
            // Get a copy of the "dgLabel0" label that contains the
            // "CustomerID" value, we'll use it in the query string
            // for the popup
            Label lblID = (Label)e.Item.FindControl("dgLabel0");
            // ...convert it to a string
            string strID = lblID.Text;
            // Replace the "Country" value displayed in the datagrid
            // with "Jamaica", placed in a hyperlink who's onClick
            // event calls a javas cript "popup" window
            e.Item.Cells[2].Text = "<a href=/"popup.aspx?id=" + strID +
                "/" onClick=/"popup(this.href); return false;/">Jamaica</a>";
        }
    }
}


而itemcreated事件和它有些不同,itemcreated发生在itemdatabound事件前,当datagrid中的每一项创建了但没
有数据时,所以不能用itemcreated事件来修改或者读取数据

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值