在GridView中处理数据不使用Data Source Controls

 
  已经有很多文章或指南介绍了在 GridView 中使用 Data Source Controls 处理数据,并且也很简单,几乎不要写什么代码,就可以实现很多功能:新增、编辑、删除、分页、排序等等,如果不使用 Data Source Controls ,也能实现这些功能吗?答案是肯定的。本文将演示不使用 Data Source Controls SQL 中的“ Northwind ”数据库中“ Employees ”表中的数据在 GridView 中显示出来。
 
以下是详细步骤:
 
1、 从工具箱中拖拽一个 GridView 控件到“设计”页面中,选择“ GridView 任务”下面的“编辑列”,向列中增加三个 BoundFields 和一个 CommandField ,然后结合图 1 修改各自属性,修改后形如图 2
图1
BoundFieldHeaderTextDataFieldReadOnly
Employee IDEmployee IDEmployeeIDtrue
First NameFirst NameFirstNamefalse
Last NameLast NameLastNamefalse


2




2 、更改页面的“ Page_Load() ”方法如下:
  protected   void  Page_Load( object  sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            BindGrid();
        }

    }





3 、在“ Page_Load() ”方法的后面添加 BindGrid() 方法:
private   void  BindGrid()
    
{
        DataSet ds 
= new DataSet();
        SqlDataAdapter da 
= new SqlDataAdapter("select * from employees"@"data source= .sqlexpress;initial catalog=northwind; integrated security=true");
        da.Fill(ds,
"employees");
        DataView dv 
= ds.Tables[0].DefaultView;

        
if (ViewState["sortexpression"!= null)
        
{
            dv.Sort 
= ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
        }


        GridView1.DataSource
=dv;
        GridView1.DataBind();
    }





4 、实现分页功能。
F4 调出 GridView 的属性页,设置“ AllowPaging ”为 True ,设置“ PageSize ”属性为 3
设置“ PageIndexChanging ”事件如下代码:
protected   void  GridView1_PageIndexChanging( object  sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;
        BindGrid();
    }




5 、实现排序功能。
选定 GridView 选择“ GridView 任务”下面的“编辑列”,设置三个“ BoundFields ”的“ SortExpression ”属性值和“ DataField ”属性相同。
设置 GridView 的“ Sorting ”事件代码如下:
protected   void  GridView1_Sorting( object  sender, GridViewSortEventArgs e)
    
{
        ViewState[
"sortexpression"= e.SortExpression;

        
if (ViewState["sortdirection"== null)
        
{
            ViewState[
"sortdirection"= "asc";
        }

        
else
        
{
            
if (ViewState["sortdirection"].ToString() == "asc")
            
{
                ViewState[
"sortdirection"= "desc";
            }

            
else
            
{
                ViewState[
"sortdirection"= "asc";
            }

        }

        BindGrid();
    }






6 、实现编辑功能。
设置 GridView 的“ RowEditing ”事件代码如下:
protected   void  GridView1_RowEditing( object  sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        BindGrid();
    }


设置GridView的“RowCancelingEdit”事件代码如下:
protected   void  GridView1_RowCancelingEdit( object  sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        BindGrid();
    }


设置GridView的“ RowUpdating”事件代码如下:
protected   void  GridView1_RowUpdating( object  sender, GridViewUpdateEventArgs e)
    
{
        
int empid;
        
string fname, lname;
        empid 
= int.Parse(GridView1.Rows[e.RowIndex].
        Cells[
0].Text);
        fname 
= ((TextBox)GridView1.Rows[e.RowIndex].
        Cells[
1].Controls[0]).Text;
        lname 
= ((TextBox)GridView1.Rows[e.RowIndex].
        Cells[
2].Controls[0]).Text;

        SqlConnection cnn 
= new SqlConnection(@"data source= .sqlexpress; initial catalog=northwind; integrated security=true");
        cnn.Open();
        SqlCommand cmd 
= new SqlCommand("update employees set firstname=@fname,lastname=@lname where employeeid=@empid",cnn);
        cmd.Parameters.Add(
new SqlParameter("@fname",fname));
        cmd.Parameters.Add(
new SqlParameter("@lname", lname));
        cmd.Parameters.Add(
new SqlParameter("@empid", empid));
        cmd.ExecuteNonQuery();
        cnn.Close();

        GridView1.EditIndex 
= -1;
        BindGrid();
    }




7、保存所有文件。按F5运行一把,效果图如下:


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值