Webcast收听笔记之二——《DataGrid最佳实践》

《DataGrid最佳实践》(邵志东主讲,他讲课很不错)
------------------------------------------------------------------------------------------------------------------
 在datagrid的删除列为了避免最后一条删除问题,应加入:
 if(DataGrid1.Items.Count=1)
 {
  if(DataGrid1.CurrentPageIndex!=0)
   DataGrid1.CurrentPageIndex=DataGrid1.CurrentPageIndex-1;
 }
------------------------------------------------------------------------------------------------------------------
 添加删除确认对话框过程:创建模板列,在模板列中加按钮并设定按钮名称与commandname(下例中设为UserDelete)等属性,
 在datagrid的itemcreated 事件中写入:
 switch(e.Item.ItemType)
 {
  case ListItemType.Item;
  case ListItemType.EditItem;
  ListItemType.AlternatingItem;
    Button myDeleteButton=(Button)e.Item.FindControl("btnDelete");
    myDeleteButton.Text="删除此列";
   //添加上删除该行的提示。下面这种从第0行开始,为了人性化可以提示为e.Item.ItemIndex+1行。
    myDeleteButton.Attributes.Add("onclick","return confirm('您真的要删除第"+e.Item.ItemIndex.ToString()+"行吗??');");
    break;
 }
 具体删除在datagrid的ItemCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)事件中写入: 
  if(e.CommandName=="UserDelete")
   DataGrid1_DeleteCommand(source,e);
------------------------------------------------------------------------------------------------------------------
 全选是在html中手动加的,剩余工作在代码中轮询,详见video45分钟左右。|||Button.Attributes.Add是加客户端属性的。
------------------------------------------------------------------------------------------------------------------
 加统计值首先设定datagrid的showfooter为true,然后循环求值并加和。然后可以做平均或者什么的。
 int count=0;
 for (int i=0;i<ds.Tables[0].Rows.Count;i++)
 {
  //求总分
  count+=int.Parse(ds.Tables[0].Rows[i]["Score"].ToString());
 }
  //求均分
 int nAv=count/ds.Tables[0].Rows.Count;
  //轮询,把需要加统计值的都加上。
 foreach(DataGridItem dgi in DataGrid1.Controls[0].Controls)
 {
 if(dgi.ItemType==ListItemType.Footer)
  dgi.Cells[6].Text="平均:"+nAv.ToString();
 }
------------------------------------------------------------------------------------------------------------------
 指定编辑状态下文本框的宽度和格式,应写在datagrid的ItemDataBound()事件中如下代码:
 if(e.Item.ItemType==ListItemType.EditItem)
 {
 for(int i=0,i<e.Item.Cells.Count;i++)
  {
   if(e.Item.Cells[i].Controls.Count>0)
   {
    try
    {
     TextBox t=(TextBox)e.Item.Cells[i].Controls[0];
     //这样定义的每个都一样宽。
     t.Width=100;
    }
    catch
    {
    }
   }
  }
 }
------------------------------------------------------------------------------------------------------------------
 隐藏列很简单,在BindGrid()时写DataGrid1.Columns[0].visible=false;即可。
------------------------------------------------------------------------------------------------------------------
 DataGrid导出成excel也很简单。两种方法:用浏览器的response方法或excel自动化方式,前者稍安全些,后者用组件方式。
 页面上加个导出按钮并在其代码中写入:
 //指定浏览器类型
 Response.ContentType="applicaion/vnd.ms-excel";
 //字符集是空
 Response.Charset="";
 this.EnalbeViewState=false;
 System.IO.StringWriter sw=new System.IO.StringWriter();
 System.Web.UI.HtmlTextWriter hw=new System.Web.UI.HtmlTextWriter(sw);
 DataGrid1.RenderControl(hw);
 //也可指定路径
 Response.Write(sw.ToString());
 Response.End();
------------------------------------------------------------------------------------------------------------------
 ItemCreated是每一项创建时执行;ItemDataBound是执行datagrid.databind时执行的。
------------------------------------------------------------------------------------------------------------------
 要往datagrid中添加一行一般用新页面处理,也可以加到dataset一行并绑定到数据库和datagrid上。
------------------------------------------------------------------------------------------------------------------
 要导pdf需要用水晶报表。
------------------------------------------------------------------------------------------------------------------
 datagrid上所有列都改成模板列实际上就是repeater了。

 

转:http://zeus.cnblogs.com/archive/2005/10/10/251974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值