GridView使用学习总结

由于Asp.Net视频比较旧,涉及到的数据绑定控件DataGrid在VS2012中已经没有了,取而代之的是GridView。开始觉得视频中的例子没法实现了,其实不然,DataGrid里面的功能GridView里一样都不少,只是形式变化了一下,仔细研究一下发现它们是换汤不换药啊。
(一)DataKeyName属性
(1)DataKeyNames一般都是用来对当前行做唯一标示的,所以一般为数据库的ID。
(2)GridView.DataKeys[e.RowIndex],e.RowIndex是获取事件对应的行,GridView.DataKeys[e.RowIndex]就是获取对应行的唯一标示也就是DataKeyNames所指定列的值。

(3)DataList和Repeater是没有的该属性的。

在代码中这样使用:(定义的该函数在下面都需要调用)

/// <summary>
/// 实现数据绑定功能
/// </summary>
private void BindToDataGird()
{
 SqlConnection con = DB.CreateCon();
 SqlDataAdapter sda = new SqlDataAdapter();
 sda.SelectCommand = new SqlCommand("select employeeID,FirstName,LastName,Title,BirthDate from employees ", con);
 DataSet ds = new DataSet();
 sda.Fill(ds, "emp");   //将查询到的数据添加到DataSet中。
 this.GridView1.DataKeyNames =new string[]{ "employeeID"}; //DataKeyNames的使用
 this.GridView1.DataSource = ds.Tables["emp"];
 this.DataBind();
}

如何取值?

DataKey key = GridView1.DataKeys[e.RowIndex];//其中e为GridViewDelete(或者Edit)EventArgs e
string empID = key[0].ToString(); 

(二)分页
由于GridView中封装了分页的功能。这里实现起来很容易。先需要设置属性:AllowPaging/PageSize/PageSetting。然后编写事件代码:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
 this.GridView1.PageIndex = e.NewPageIndex;
 this.BindToDataGird();
} 

(三)排序
首先设置AllowSorting属性为true.事件代码:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
 if (ViewState["order"] == null)  //使用ViewState设置双向排序。
 {
  ViewState["order"] = "ASC";
 }
 else
 {
  if (ViewState["order"].ToString() == "ASC")
  {
   ViewState["order"] = "DESC";
  }
  else
  {
   ViewState["order"] = "ASC";
  }
 }
 //数据绑定显示
 SqlConnection con = DB.CreateCon();
 SqlDataAdapter sda = new SqlDataAdapter();
 sda.SelectCommand = new SqlCommand("select employeeID,FirstName,LastName,Title,BirthDate from employees ", con);
 DataSet ds = new DataSet();
 sda.Fill(ds, "emp");
 ds.Tables["emp"].DefaultView.Sort = e.SortExpression + " " + ViewState["order"].ToString(); //设置排序
 this.GridView1.DataSource = ds.Tables["emp"].DefaultView; //将表的默认视图作为数据源。
 this.DataBind();
} 

(四)删除
这里需要注意一点:就是获取某一行的主键值。

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
 DataKey key = GridView1.DataKeys[e.RowIndex];
 string empID = key[0].ToString();
 SqlConnection con = DB.CreateCon();
 SqlCommand cmd = new SqlCommand("delete from employees where employeeID= '"+empID+"'" , con);
 con.Open();
 cmd.ExecuteNonQuery();
 this.BindToDataGird();
}

(五)编辑(更新和取消)

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
 this.GridView1.EditIndex = e.NewEditIndex;
 this.BindToDataGird();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
 this.GridView1.EditIndex = -1; //设置索引值为负取消编辑。
 this.BindToDataGird();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
 DataKey key = GridView1.DataKeys[e.RowIndex];
 string empID = key[0].ToString();
 string lastName=((TextBox)(GridView1.Rows [e.RowIndex ] .Cells [2].Controls [0])).Text ; //将GridView中某列中控件强制转换为TextBox,然后取出它的值。
 Response.Write(empID +"&" + lastName ); //用于测试。
 this.GridView1.EditIndex = -1;
 this.BindToDataGird();
}

附结果图:

小结:数据绑定控件:Reapter/DataList/GridView的功能成递增关系,都使用到了模板。所以掌握模板很重要。视频使用模板大都是使用控件,不是代码。总感觉这里需要学习的地方还有很多。需要做例子巩固使用。

(0)

相关推荐

  • DataGridView展开与收缩功能实现

    很多数据都有父节点与子节点,我们希望单击父节点的时候可以展开父节点下的子节点数据. 比如一个医院科室表,有父科室与子科室,点击父科室后,在父科室下面可以展现该科室下的所有子科室. 我们来说一下在DataGridView中如何实现这个功能. 首先,创建示例数据: 示例数据SQL create table Department ( ID int identity(1,1) not null, DName varchar(20) null, DparentId int null, Dtelphone

  • C#处理datagridview虚拟模式的方法

    本文实例讲述了C#处理datagridview虚拟模式的方法.分享给大家供大家参考.具体如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace VirtualMode { partial class

  • 如何用jQuery实现ASP.NET GridView折叠伸展效果

    今天做静态页面时有一个需求,就是页面上有一组两个选项的单选按钮和一个有6行的列表(该列表用Table标签实现,不是DIV),当选择单选按钮的选项一时,列表的前三条信息显示后三条信息隐藏,当选择单选按钮的选项二时,列表的前三条信息隐藏后三条信息显示.那么就牵扯出我们今天的话题拉,如何实现呢?实现后该实现还能应用到哪些场景? 1.第一反应的解决方案 碰到这个需求后,我第一反应就是很简单啊,分别用两个DIV将前三个Table中的TR标签与后三个TR标签包起来,然后通过JS控制DIV的显示. 第一步:使

  • C#中GridView动态添加列的实现方法

    本文实例讲述了C#中GridView动态添加列的实现方法.分享给大家供大家参考.具体如下: protected void Page_Load(object sender, EventArgs e) { TemplateField mycustomField = new TemplateField(); //创建列实例 mycustomField.ShowHeader = true; // 设置属性 LinkButton lb = new LinkButton(); lb.Text = "Dele

  • ASP.NET GridView中加入RadioButton不能单选的解决方案

    今天开发碰见一个问题,就是当GridView中加入一个包含RadioButton的模板列,结果一运行.....天啊,单选按钮可以多选了! 囧啊!为了演示一下我今天的错误我还是模拟一个功能场景吧,我要实现的功能是显示一个包含单选按钮的学生信息列表,选择一行后将详细信息显示出来~! 1.问题展现 ①首先准备一个GridView用来展示学生的基本信息与最重要的单选按钮,代码如下: <asp:GridView ID="GridView1" runat="server"

  • C#实现DataGridView控件行列互换的方法

    本文实例讲述了C#实现DataGridView控件行列互换的方法.分享给大家供大家参考.具体如下: 该示例程序是一个Windows窗体应用程序,有左右两个DataGridView控件:dgvLeft和dgvRight dgvRight除时间外的每一行是dgvLeft的一列 private void Form1_Load(object sender, EventArgs e) { //C#中确定控件DataGridView根据内容自动调整列宽长度的属性 //是AutoSizeColumnsMode

  • C#中DataGridView动态添加行及添加列的方法

    本文实例讲述了C#中DataGridView动态添加行及添加列的方法.分享给大家供大家参考.具体如下: Datagridview添加列: DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn(); acCode.Name = "acCode"; acCode.DataPropertyName = "acCode"; acCode.HeaderText = "A/C Code&quo

  • C#中datagridview的EditingControlShowing事件用法实例

    本文实例讲述了C#中datagridview的EditingControlShowing事件用法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using datagridv

  • C#实现绑定DataGridView与TextBox之间关联的方法

    本文实例讲述了C#实现绑定DataGridView与TextBox之间关联的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace System.Window

  • asp.net中GridView数据鼠标移入显示提示信息

    问题提出: 在asp.net开发中,如果有这样的一个需求,如果在列表控件,如GridView中的某列中显示的是一个计算公式得出的值,那么需求来了,鼠标移入该数字,显示该数字的计算公式和过程,如何做? 解决方案分析: 常规可以使用控件的title属性来显示提示信息,但是显示信息的样式不美观.接下来我们可以使用这样的一个解决方案,其显示效果如下图所示: 详细实现步骤: 1.下载弹出提示框相关js文件包,下载地址:http://download.csdn.net/detail/taomanman/90

  • C#实现3步手动建DataGridView的方法

    本文实例讲述了C#实现3步手动建DataGridView的方法.分享给大家供大家参考.具体如下: DataView dvCmControlInfo =  new  DataView(某个DataTable); 第一步 dataGridView1.Columns.Clear(); dataGridView1.AutoGenerateColumns = false ; dataGridView1.DataSource = dvCmControlInfo; //绑DataTable,DataSet,D

  • C#中DataGridView常用操作实例小结

    本文实例讲述了C#中DataGridView常用操作.分享给大家供大家参考.具体如下: public void Binder1() { DataTable tableType = DataBase.SQLDBHelper.GetDataTable("select top 200 unit_code,unit_name from unit "); DataTable table = DataBase.SQLDBHelper.GetDataTable("select top 2

  • GridView自动增加序号(三种实现方式)

    第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了. 复制代码 代码如下: <asp:TemplateField HeaderText="序号" InsertVisible="False"> <ItemTemplate> <%#Container.DataItemIndex+1%> </ItemTemplate> </asp:TemplateField> 第二种方

  • GridView控件如何显示序号

    在ASP.NET 中,如果想 GridView 列表中显示序列号,不需要自己编写获取序号的方法,通过Container.DataItemIndex 属性加 1 就可以实现了,绑定在列表中就 OK 了! 实例代码: <asp:GridView ID="gvList" runat="server" Width="100%"> <Columns> <asp:TemplateField HeaderText="序号

随机推荐