文谷首页 | 业界传真 | 网络技术 | 服务器 | 数据库 | 存储技术 | 系统安全 | 无线技术 | Cisco | .Net | Windows | Linux | Unix | Java
电子商务 | 网站工程 | 网页设计 | 平面设计 | 多媒体 | 编程语言 | Oracle | MSSQL | Photoshop | ASP | PHP | 实用技巧 | 进程查询 | 文谷论坛
 websphere   .net framework
您现在的位置: IT文谷 >> 开发平台 >> .net framework >> ADO.NET >> 文章正文
DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)
DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)
DataGrid使用心得(附大量代码)

DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)


 
 8.为DataGrid控件添加分页事件

  在DataGrid控件标签中加入如下代码
  OnPageIndexChanged="DataGrid1_PageIndexChanged"
  在后台中加入如下代码
  ///<summary>
  ///响应分页事件
  ///</summary>
  ///<paramname="sender"></param>
  ///<paramname="e"></param>
  publicvoidDataGrid1_Page(Objectsender,DataGridPageChangedEventArgse)
  {
   DataGrid1.CurrentPageIndex=e.NewPageIndex;
   DataBind();
  }
 
 9.为DataGrid控件添加绑定事件,即在DataGrid绑定时发生的事件处理

  一般用些事件来,做一些页面上的效果.如更改背景色,文本框大小等.
  OnItemDataBound="DataGrid1_ItemDataBound"
  ///<summary>
  ///响应DataGrid绑定事件
  ///</summary>
  ///<paramname="sender"></param>
  ///<paramname="e"></param>
  publicvoidDataGrid1_ItemDataBound(objectsender,System.Web.UI.WebControls.DataGridItemEventArgse)
  {
   if(e.Item.ItemType==ListItemType.Item)
   {
    e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#c8dafa'");
    e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='white'");

   }
   elseif(e.Item.ItemType==ListItemType.AlternatingItem)
   {
    e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#c8dafa'");
    e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='#f6f6f6'");
   }
  }
  
 10.为DataGrid控件添加接钮处理事件程序

  在DataGrid控件标签中加入如下代码
  OnItemCommand="ItemsGrid_Command"
  在后台中加入如下代码
  publicvoidItemsGrid_Command(Objectsender,DataGridCommandEventArgse)
  {
   switch(((LinkButton)e.CommandSource).CommandName)
   {

    case"Delete":
     intclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
     ActorClass.DeleteActorClass(classID);
     if(Request.QueryString.Get("classID")!=null)
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
     else
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
     break;

     //Addothercaseshere,iftherearemultipleButtonColumnsin
     //theDataGridcontrol.
    case"hidden":
     intactorID=Int32.Parse((e.Item.Cells[0].Text).ToString());
     ActorClass.HiddenActorClass(actorID);
     if(Request.QueryString.Get("classID")!=null)
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
     else
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
     break;
    case"MoveUp":
     intactorclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
     stringorderID=(e.Item.Cells[5].Text).ToString();
     ActorClass.MoveUp(orderID,actorclassID);
     if(Request.QueryString.Get("classID")!=null)
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
     else
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbydepth,orderIDdesc"));
     break;
    case"MoveDown":
     actorclassID=Int32.Parse((e.Item.Cells[0].Text).ToString());
     orderID=(e.Item.Cells[5].Text).ToString();
     ActorClass.MoveDown(orderID,actorclassID);
     if(Request.QueryString.Get("classID")!=null)
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswhereparentID="+Request.QueryString.Get("classID")+"orderbydepth,orderIDdesc"));
     else
      Common.BindData(DataGrid1,Common.GetSource("select*fromActorClasswheredepth=1orderbyorderID"));
     break;
    

    default:
     //Donothing.
     break;

   }
  }

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

  以下三个都是,根据字段列不同,而显示内容及显示控件不同的处理代码.
  <asp:TemplateColumnHeaderText="子菜单">
   <ItemTemplate>
    <%#ActorClassManage.hasLeaf(DataBinder.Eval(Container.DataItem,"ActorClassID").ToString(),DataBinder.Eval(Container.DataItem,"child").ToString())%>
   </ItemTemplate>
  </asp:TemplateColumn>
  publicstaticstringhasLeaf(stringid,stringchild)
  {
   stringlRtn="";
   if(Int32.Parse(child)>0)
    lRtn="<ahref='Actorclassmanage.aspx?classID="+id+"'><fontcolor=blue>子菜单("+child+")</font></a>";
   else
    lRtn="无子菜单";

   returnlRtn;
  }
  <asp:TemplateColumnHeaderText="属性">
   <ItemTemplate>
    <asp:LinkButtonText='<%#IsHidden(DataBinder.Eval(Container.DataItem,"ActorClassID").ToString(),(bool)DataBinder.Eval(Container.DataItem,"Enabled"))%>'runat="server"CommandName="hidden"ID="Linkbutton1"></asp:LinkButton>
   </ItemTemplate>
  </asp:TemplateColumn>
  publicstaticstringIsHidden(stringid,boolenabled)
  {
   stringlRtn="";
   if(enabled==true)
   {
    lRtn="[显示]";
   }
   else
   {
    lRtn="隐藏";
   }
   returnlRtn;
  }
  publicstaticvoidSort(stringactorclassID,stringorderID)
  {
   stringtemp="";
   if(Int32.Parse(BgPicManage.GetMaxCode("actorclass","orderID"))==Int32.Parse(orderID))
   {
    temp+="<ipnuttype='submit'value='向下移'>";
   }
   if(Int32.Parse(orderID)==0)
   {
    temp+="<ipnuttype='submit'value='向上移'>";
   }
  }
  
 12.DataGrid控件自定义分页代码
 
  将下列代码放于包含<DataGrid>的form中去,
  <pstyle="FONT-SIZE:9pt"align="center">
         <asp:labelid="lblPageCount"runat="server"></asp:label>
         <asp:labelid="lblCurrentIndex"runat="server"></asp:label>
         <asp:linkbuttonid="btnFirst"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
          Font-size="8pt"ForeColor="navy"CommandArgument="0"></asp:linkbutton>
         <asp:linkbuttonid="btnPrev"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
          Font-size="8pt"ForeColor="navy"CommandArgument="prev"></asp:linkbutton>
         <asp:linkbuttonid="btnNext"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
          Font-size="8pt"ForeColor="navy"CommandArgument="next"></asp:linkbutton>
         <asp:linkbuttonid="btnLast"onclick="PagerButtonClick"runat="server"Font-Name="verdana"
          Font-size="8pt"ForeColor="navy"CommandArgument="last"></asp:linkbutton>
       </p>
       
       后台代码
       
       privatevoidPage_Load(objectsender,System.EventArgse)
       {
         //在此处放置用户代码以初始化页面
         btnFirst.Text="最首页";
         btnPrev.Text="前一页";
         btnNext.Text="下一页";
         btnLast.Text="最后页";
         //绑定数据源
         if(!Page.IsPostBack)
         {
          OpenDatabase();
          BindGrid();
         }
       }
       
       //用于显示"第几页,总*页"
       privatevoidShowStats()
  {
    lblCurrentIndex.Text="第"+(MyDataGrid.CurrentPageIndex+1).ToString()+"页";
    lblPageCount.Text="总共"+MyDataGrid.PageCount.ToString()+"页";
  }
  
  //响应分页按钮
  publicvoidPagerButtonClick(objectsender,EventArgse)
       {
        stringarg=((LinkButton)sender).CommandArgument.ToString();
         switch(arg)
         {
           case"next":
             if(MyDataGrid.CurrentPageIndex<(MyDataGrid.PageCount-1))
             {
              MyDataGrid.CurrentPageIndex+=1;
            }
            break;
           case"prev":
             if(MyDataGrid.CurrentPageIndex>0)
             {
              MyDataGrid.CurrentPageIndex-=1;
             }
             break;
           case"last":
             MyDataGrid.CurrentPageIndex=(MyDataGrid.PageCount-1);
             break;
           default:
             MyDataGrid.CurrentPageIndex=System.Convert.ToInt32(arg);
             break;
         }
         BindGrid();
         ShowStats();
       }

上一页  [1] [2] 

上一页  [1] [2] 

DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)


  • 上一篇文章:

  • 下一篇文章: 没有了
  • 进入论坛讨论

    相关文章
    基于.NET的多线程编程入门
    Microsoft .NET 框架资源基础
    VS2005常用插件搜罗
    .net中用GDI+提高gif图片保存画质
    VS.NET:通过Web服务瞄准电子商务
    对象设计中创建VS使用
    安装/初始化/个性化DotNetNuke
    介绍dotnet原动力(DNN)
    GDI+编程10个基本技巧
    DTE .NET工程的自动化编译
    .Net中如何操作IIS的原理分析
    VS.NETRC5无法创建Add-IN工程
    热门文章最新推荐

    版权与免责声明:
    ① 本网转载其他媒体稿件是为传播更多的信息,此类稿件不代表本网观点,版权归原作者所有,本网不承担此类稿件侵权行为的连带责任。
    ② 在本网BBS上发表言论者,文责自负。
    ③ 如您因版权等问题需要与本网联络,请在30日内联系 。
    DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)
    DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)2006-7-15DataGrid使用心得(附大量代码)

    .net framework专题
    人气排行
  • 此栏目下没有文章
  • 最近更新
    普通文章技术介绍 新数据网格简介
    普通文章.NET 开发人员该下载的十个必备
    普通文章深入剖析Asp.net资源文件
    普通文章通过事例学习.net的WebForms技
    普通文章ASP.NET Tips1---合并多个字段
    普通文章微软明年1月份出台.NET专业开发
    普通文章微软下月底前完成Vista大部分功
    普通文章揭开微软 .NET 认证考试的面纱
    普通文章Visual Studio:Microsoft开发工
    普通文章Visual Studio:Microsoft开发工
    全站热点       
    最新推荐
    关于文谷 | 联系文谷 | 免责声明 | 文谷社区
    Tel: 0577-65690019    E-mail: ichenjian@gmail.com    MSN:ichenjian@hotmail.com    QQ:2911194
    Copyright © 2004-2006 wengu.org 文谷 All Rights Reserved
    浙ICP备05000327号