gridview 基本使用

来源:互联网 发布:数据库课程设计web源码 编辑:程序博客网 时间:2024/06/10 07:35

 前台代码:

  1.  <asp:GridView runat="server" ID="grdText" AutoGenerateColumns="False" OnRowCancelingEdit="grdText_RowCancelingEdit"
  2.                 OnRowDeleting="grdText_RowDeleting" OnRowEditing="grdText_RowEditing" OnRowUpdating="grdText_RowUpdating"
  3.                 OnRowDataBound="grdText_RowDataBound">
  4.                 <Columns>
  5.                     <asp:BoundField HeaderText="ID" DataField="Id" ReadOnly="true" />
  6.                     <asp:BoundField HeaderText="姓名" DataField="Name" ReadOnly="true"/>
  7.                     <asp:BoundField HeaderText="年龄" DataField="Age" ReadOnly="true"/>
  8.                     
  9.                     <asp:TemplateField HeaderText="学校">
  10.                         <EditItemTemplate>
  11.                             <asp:DropDownList runat="server" ID="dropSchool">
  12.                             </asp:DropDownList>
  13.                         </EditItemTemplate>
  14.                         <ItemTemplate>
  15.                             <%# Eval( "SchoolName" ) %>
  16.                         </ItemTemplate>
  17.                     </asp:TemplateField>
  18.                     <asp:TemplateField>
  19.                        <ItemTemplate>
  20.                             <asp:HiddenField runat="server" ID="hiddenSchoolId" Value=<%# Eval( "SchoolId" ) %> />
  21.                        </ItemTemplate>
  22.                     </asp:TemplateField>
  23.                     <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
  24.                     <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
  25.                 </Columns>
  26.             </asp:GridView>

后台代码:

 

  1. DatebaseClass objDB = new DatebaseClass();
  2.     protected void Page_Load(object sender, EventArgs e)
  3.     {
  4.         Response.Expires = 0;
  5.         if( !IsPostBack )
  6.         {
  7.             this._BeginRun();
  8.         }
  9.     }
  10.     private void _BeginRun()
  11.     { 
  12.         DataTable dt = objDB.getStudent();
  13.         if( dt != null )
  14.         { 
  15.             this.grdText.DataSource = dt;
  16.             this.grdText.DataKeyNames = new string []{ "Id" };
  17.             this.grdText.DataBind();
  18.         }
  19.     }
  20.     protected void grdText_RowDeleting( object sender, GridViewDeleteEventArgs e )
  21.     {
  22.         objDB.delete( Convert.ToInt32( grdText.DataKeys[ e.RowIndex ].Value )  );
  23.         _BeginRun();
  24.     }
  25.     /// <summary>
  26.     /// 行编辑
  27.     /// </summary>
  28.     /// <param name="sender"></param>
  29.     /// <param name="e"></param>
  30.     protected void grdText_RowEditing( object sender, GridViewEditEventArgs e )
  31.     {
  32.         grdText.EditIndex = e.NewEditIndex;
  33.         _BeginRun();
  34.     }
  35.     /// <summary>
  36.     /// 取消行编辑
  37.     /// </summary>
  38.     /// <param name="sender"></param>
  39.     /// <param name="e"></param>
  40.     protected void grdText_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e )
  41.     {
  42.         grdText.EditIndex = -1;
  43.         _BeginRun();
  44.     }
  45.     /// <summary>
  46.     /// 行修改
  47.     /// </summary>
  48.     /// <param name="sender"></param>
  49.     /// <param name="e"></param>
  50.     protected void grdText_RowUpdating( object sender, GridViewUpdateEventArgs e )
  51.     {
  52.         int schoolId = Convert.ToInt32( ( grdText.Rows[ e.RowIndex ].Cells[4].Controls[0] as TextBox ).Text.Trim() );
  53.         objDB.update( Convert.ToInt32( grdText.DataKeys[ e.RowIndex ].Value ), schoolId  );
  54.         grdText.EditIndex = -1;
  55.         _BeginRun();
  56.     }
  57.     /// <summary>
  58.     /// 邦定学校
  59.     /// </summary>
  60.     /// <param name="drop"></param>
  61.     protected void binderDropSchool( DropDownList drop )
  62.     { 
  63.         drop.DataSource = objDB.getScholl();
  64.         drop.DataTextField = "SchoolName";
  65.         drop.DataValueField = "Id";
  66.         drop.DataBind();
  67.     }
  68.     /// <summary>
  69.     /// rowDataBound
  70.     /// </summary>
  71.     /// <param name="sender"></param>
  72.     /// <param name="e"></param>
  73.     protected void grdText_RowDataBound( object sender, GridViewRowEventArgs e )
  74.     {
  75.         if( e.Row.RowType == DataControlRowType.DataRow )
  76.         { 
  77.             int i = e.Row.Cells.Count -1 ;
  78.             DropDownList drop = e.Row.FindControl( "dropSchool" ) as DropDownList;
  79.             if( drop != null )
  80.             {
  81.                 HiddenField hid = e.Row.FindControl( "hiddenSchoolId" ) as HiddenField;
  82.                 binderDropSchool( drop );
  83.                 if( hid != null )
  84.                 {
  85.                     drop.SelectedValue = hid.Value;
  86.                 }
  87.             }
  88.             e.Row.Attributes.Add( "onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#E6F5FA'" );
  89.             e.Row.Attributes.Add( "onmouseout""this.style.backgroundColor=c" );
  90.             if ( e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate )
  91.             {
  92.                 e.Row.Cells[i].Attributes.Add( "onclick""javascript:return confirm('你确认要删除!')");
  93.             }
  94.         }
  95.     }

记录备用!(Data Source=127.0.0.1;Initial Catalog=llc2007;User ID=sa;Password=were)

 

 

 

 

原创粉丝点击