GRIDVIEW 控件
http://www.cnblogs.com/shanymen/archive/2009/05/22/1486654.html
GridView控件是.net里的一个显示数据控件,该控件制作很人性化,基本上不用编写代码就可以完成数据绑定、分页、排序、编辑、删除、选定行等操作。
主要属性:
Sort:根据指定的排序表达式和方向对 GridView 控件进行排序。
- 编程的方法绑定数据,并实现分页
页面源代码中添加一个GridView控件(GridView1),并AllowPaging="True",设置PageIndexChanging事件。
cs代码:
protected void bind()
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.bind();
}
- 编程方法实现排序
页面源代码中添加一个GridView控件(GridView1),并设置GridView1的Sorting事件。
cs代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//设置信息字典,排序字段和排序方法
ViewState["SortOrder"] = "ID";
ViewState["OrderDire"] = "ASC";
this.bind();
}
}
public SqlConnection GetConnection()
{
//读取web.config中的连接字符串,创建SqlConnection连接
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs); //设置排序所用的字段和排序方法
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
GridView1.Sort = sort;
GridView1.DataSource = myDs;
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{ //获取排序的字段
string sPage = e.SortExpression; //如果目前排序方式与设置一致,则逆序
if (ViewState["SortOrder"].ToString() == sPage)
{
if (ViewState["OrderDire"].ToString() == "Desc")
{
ViewState["OrderDire"] = "ASC";
}
else
{
ViewState["OrderDire"] = "Desc";
}
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
this.bind();
}
- 选择GridView控件的行,在另一个GridView控件中显示相关数据
页面源代码中添加两个GridView控件(GridView1,GridView2),并设置GridView1的SelectedIndexChanging事件。GridView1已经绑定了一个数据表,当点击GridView1一行时,在GridView2中显示相关的另一张表中的数据。
cs代码:
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//提交编辑时,先取得要编辑行的主键值
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string sqlStr = "delete from test where ID=" + ID;
SqlConnection con = new SqlConnection();
//读取web.config中的连接字符串,创建SqlConnection连接
con.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
//将数据表绑定到GridView2
this.GridView2.DataSource = myDs;
GridView2.DataBind();
}
- 编程实现全选和全不选功能
页面源代码中添加两个GridView控件(GridView1),一个CheckBox控件(CheckBox1),设置CheckBox控件的AutoPostBack="True" ,为GridView1添加一个TemplateField列,并在编辑模版中为该列添加一个CheckBox 控件:<asp:TemplateField><ItemTemplate><asp:CheckBox ID="Check" runat="server" /></ItemTemplate></asp:TemplateField>。设置CheckBox的CheckedChanged事件。
cs代码:
Code
- 鼠标移动到任意行时,该行自动变成指定颜色,双击打开新页
cs代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到任意行时,该行自动变成指定颜色
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#BEC9F6';this.style.color='buttontext';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='';this.style.color='';");
//双击打开新页
e.Row.Attributes.Add("onclick", "window.open('FileInfo.aspx?id="+e.Row.Cells[0].Text+"')");
}
}
GRIDVIEW 控件的更多相关文章
- 扩展GridView控件——为内容项添加拖放及分组功能
引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...
- .Net语言 APP开发平台——Smobiler学习日志:用Gridview控件设计较复杂的表单
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobil ...
- GridView控件隐藏列
GridView隐藏列visible="false" 后你就无法取得这列的值了 下面是迄今为止最简洁的解决方法了. protected void GVList_RowDataBou ...
- GridView控件中加自动排列序号
GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField HeaderText="序号"> < ...
- asp.net GridView控件的列属性
BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...
- ASP.NET中GridView控件删除数据的两种方法
今天在用GridView控件时,发现了一个问题,就是使用GridView控件在删除数据时的问题.接下来我们通过模板列方式和CommandField方式删除某条数据讲解下两者之间的区别. 方式一:通 ...
- 027. asp.net中数据绑定控件之 GridView控件
GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...
- GridView控件 Reapter控件 DataList控件 的区别和用法
ASP.NET三大控件: 1.GridView控件:表格视图控件,可以用来绑定结果集或者视图,用起来比较方便和灵活,三个控件中使用最多的控件 用法--- this.gridview1.DataSour ...
- GridView控件RowDataBound事件中获取列字段值的几种途径
前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...
随机推荐
- LeetCode189——Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- iOS 自定义emoji表情键盘
之前走了很多弯路,包括自己定以emoji表情,自己创建view类去处理图文混排 ,当把这些焦头烂额的东西处理完了才发现 ,其实系统自带键盘是如此的方便,iOS 系统自带的表情在view,textfie ...
- 1、webservice的简单使用
1.新建一个web端项目 2.点击添加项,选择web服务 3.在已经建好的项目中写一个方法 4.发布(发布方法选文件系统,web需要管理员权限) 生成文件夹: 5.配置IIS(略) 6.调用webse ...
- maven web project打包为war包,目录结构的变化
一个maven web project工程目录: 资源管理器中的目录层级如下: 导出为war包之后的目录层级为: 我们会发现,其实并没有如下的这些目录层级: 所以这两个目录层级只是IDE为我们添加的, ...
- Anchor 对象和document对象
<script type="text/javascript"> function chanklink(){ document.getElementById(" ...
- viewpager接受值图片轮播
package com.baway.test; import java.util.ArrayList;import java.util.List;import java.util.Timer;impo ...
- Codeforces Round #161 (Div. 2)
A. Beautiful Matrix 即相当于求1到中心位置\((2,2)\)的曼哈顿距离. B. Squares 排序,取倒数第\(k\)个即可. C. Circle of Numbers 固定\ ...
- win10 64bits信捷触摸屏download usb口驱动程序的安装
[问题描述] win10 64bits,信捷触摸屏开发软件,下载的时候需要安装驱动程序. 用的以前的win7 64bits的驱动程序,直接双击安装,显示无法操作注册表,需要管理员模式. 使用管理员权限 ...
- js 日期按年月日加减
<script> function isleapyear(year) { if(parseInt(year)%4==0 && parseInt(year)%100!=0)r ...
- AsyncTask 轻量级的异步类
初步:http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html 详细:http://blog.csdn.net/liuhe68 ...