gridview 基本使用

 前台代码:

  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)

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
GridView是Android中常用的布局控件之一,可以实现网格状的布局。以下是GridView使用方法: 1. 在XML布局文件中添加GridView控件: ``` <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="3" /> ``` 2. 在Java代码中获取GridView控件并设置适配器: ``` GridView gridView = findViewById(R.id.gridview); gridView.setAdapter(new MyAdapter()); ``` 3. 创建适配器MyAdapter,并实现getView方法: ``` class MyAdapter extends BaseAdapter { private Context mContext; public MyAdapter(Context context) { mContext = context; } @Override public int getCount() { return 9; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); } else { imageView = (ImageView) convertView; } //设置图片资源 imageView.setImageResource(R.drawable.image); return imageView; } } ``` 在getView方法中,我们可以设置每个网格的样式和内容,这里以显示图片为例。 以上就是GridView基本使用方法。需要注意的是,如果网格较多,可以考虑使用RecyclerView来实现更好的性能和滑动体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值