GridView学习记录(一)
今天学习了GridView中的一些功能,主要包括数据的绑定,删除,更新。使用GridView 中的 RowDeleting, RowEditing,RowCancelingEditing,RowUpdating.
后台代码(以下代码并非完成代码,只是介绍每个事件,属性,方法应该如何使用)
1. GridView 的数据绑定涉及到的属性包括: DataSource, DataKeyNames(使用GridView中的事件 必须要设定这个数值) 。以及事件 DataBind()
代码如下:
1 private void GvBind() 2 { 3 DataTable dtCustome = testOperate.SearchReturnTable();//查询数据集,返回DataTable。 4 GridView.DataSource = dtCustome; 5 GridView.DataKeyNames = new string[] {"id"};//设置GridView中数据的主键字段。 6 GridView.DataBind(); 7 }
GridView绑定数据
注意:如果使用GridView中的更新,删除必须设置DataKeyNames属性。
2. 删除数据,GridView删除数据需要用到RowDeleting事件。假设GridView控件名称:GridTest
代码如下:
1 protected void GridTest_RowDeleting(object sender, GridViewDeleteEvent e) 2 { 3 int id = (int) GridTest.DataKeys[e.RowIndex].Value;//如果绑定数据中没有设置DataKeyNames,此处系统会报错。 4 testOperate.DeleteInfo(id); //所写的删除信息方法。 5 GvBind(); 6 }
GridView中RowDeleting事件
3.更新数据,GridView更新数据需要用到RowEditing, RowCancelingEditing与RowUpdating三个事件。
代码如下:
1 protected void GridTest_RowEditing(object sender, GridViewEditEventArgs e) 2 { 3 GridTest.EditIndex = e.NewEditIndex; 4 GvBind(); 5 } 6 7 protected void GridTest_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 8 { 9 GridTest.EditIndex = -1; 10 GvBind(); 11 } 12 13 protected void GridTest_RowUpdating(object sender, GridViewUpdateEventArgs e) 14 { 15 16 GridViewRow row = GridTest.Rows[e.RowIndex]; 17 18 //获得所在Row的主键值 19 DataKey key = GridTest.DataKeys[e.RowIndex]; 20 int id = Convert.ToInt32(key[0].ToString()); 21 22 //需要更新的信息 23 string content = ((TextBox)(row.Cells[1].Controls[0])).Text; 24 string state =((TextBox)(row.Cells[3].Controls[0])).Text; 25 26 //编写的更新数据信息方法; 27 testOperate.UpdateInfo(content, Convert.ToInt32(state), id); 28 GVnotices.EditIndex = -1; 29 GvBind(); 30 } 31 }
GridView更新数据
前台代码:
<asp:GridView ID="GVnotices" runat="server" AutoGenerateColumns="false" OnRowDeleting="GridTest_RowDeleting" OnRowEditing="GridTest_RowEditing" OnRowCancelingEdit="GridTest_RowCancelingEdit" OnRowUpdating="GridTest_RowUpdating"><Columns><asp:BoundField DataField="id" HeaderText="编号" ReadOnly="true"/><asp:BoundField DataField="content" HeaderText="内容" ReadOnly="false"/><asp:BoundField DataField="creatNo" HeaderText="录入时间" ReadOnly="true" /><asp:BoundField DataField="state" HeaderText="是否显示" ReadOnly="false"/> <asp:CommandField ShowCancelButton="true" ShowEditButton="true" ShowDeleteButton="true" EditText="编辑" UpdateText="更新" DeleteText="删除" CancelText="取消" HeaderText="操作" /> </Columns></asp:GridView>
在前台代码中需要注意的几个问题:
1.如果希望自定义显示数据的顺序在GridView中 需要将 AutoGenerateColumns设置为False。否则绑定数据将按照传入DataTable中的顺序显示。
2.如果在更新中有不需要更新的列可将 <asp: BoundField>中的 ReadOnly设置为True, 默认情况为False;
3.如果希望见”编辑“,”删除“操作放在同一列下,只需添加一个<asp:CommandField>,并且设置 属性 ShowCancelButton, ShowEditButton, ShowDeleteButton为True。
4.更改显示Button 的内容可以通过EditText, UpdateText, DeleteText与CancelText来设置,设置列名可以通过HeaderText属性来设置。
以上内容为本人自己学习总结,或许有不足,不当之处各位指出。
转载于:https://www.cnblogs.com/Weimin496/p/8073062.html
GridView学习记录(一)相关推荐
- Android Studio学习记录之简单的页面切换及宫格菜单
Android Studio学习记录之简单的页面切换及宫格菜单 之前上课听老师讲的一些东西自己其实并没有消化,今天把不懂的都去网上搜了一下,有了一种恍然大悟的感觉,包括很多方方面面的东西.有些东西听说 ...
- Pytorch学习记录-torchtext和Pytorch的实例( 使用神经网络训练Seq2Seq代码)
Pytorch学习记录-torchtext和Pytorch的实例1 0. PyTorch Seq2Seq项目介绍 1. 使用神经网络训练Seq2Seq 1.1 简介,对论文中公式的解读 1.2 数据预 ...
- HTML5与CSS3权威指南之CSS3学习记录
title: HTML5与CSS3权威指南之CSS3学习记录 toc: true date: 2018-10-14 00:06:09 学习资料--<HTML5与CSS3权威指南>(第3版) ...
- springboot @cacheable不起作用_Springboot学习记录13 使用缓存:整合redis
本学习记录的代码,部分参考自gitee码云的如下工程.这个工程有详尽的Spingboot1.x教程.鸣谢! https://gitee.com/didispace/SpringBoot-Learnin ...
- 【Cmake】Cmake学习记录
Cmake学习记录 1.1 常例 add_library(gen_reference_infogen_reference_info/gen_reference_info.hgen_reference_ ...
- ASP.NETCore学习记录(一)
ASP.NETCore学习记录(一) asp.net core介绍 Startup.cs ConfigureServices Configure 0. ASP.NETCore 介绍 ASP.N ...
- Android开发技术周报176学习记录
Android开发技术周报176学习记录 教程 当 OkHttp 遇上 Http 2.0 http://fucknmb.com/2018/04/16/%E5%BD%93OkHttp%E9%81%87% ...
- add函数 pytorch_Pytorch学习记录-Pytorch可视化使用tensorboardX
Pytorch学习记录-Pytorch可视化使用tensorboardX 在很早很早以前(至少一个半月),我做过几节关于tensorboard的学习记录. https://www.jianshu.co ...
- java之字符串学习记录
java之字符串学习记录 public class StringDemo { public static void main(String[] args) { //静态初始化字符串 String s1 ...
最新文章
- Meta 2022博士奖学金计划公布,华人学者占四成
- 【OpenCV3】级联分类器目标检测——cv::CascadeClassifier简介
- SAP UI5中的同步请求和异步请求
- sql几种删除语句的联系与区别
- Sentinel(十四)之控制台
- 人工智能是未来发展趋势吗 用Python入门怎么样 赶紧看看
- 图注意力网络_EMNLP 2019开源论文:针对短文本分类的异质图注意力网络
- java 线程 内部_从Java中的main()内部在线程实例上运行wait()
- 嵌入式基础面八股文——孤儿进程、僵尸进程、守护进程的概念(3)
- 带你玩转Visual Studio——带你跳出坑爹的Runtime Library坑
- Fbinst增强版 命令详解
- Faster R-CNN论文翻译——中英文对照
- 这些前端资源,你值得拥有
- 云栖大会人脸识别闸机【技术亮点篇4】--户外闸机高20%的抗撞击能力
- strcpy_s与strcpy
- 【佳学基因人工智能解码技术系列】在CENTOS环境下安装ANACOND
- 服务器机器人维修技术,维修机器人
- MCU集成-时钟复位控制
- HTTP长链接与短连接?
- [LeetCode] 两数之和