web控件Gridview简单操作

       在工具箱中拖入一个gridview到页面上,右侧可以看到有一个向右的小箭头,称为智能标记。点开它,第二个是编辑列单击,出现一个关于字段的对话框,在commandfield里面将编辑、更新、取消添加上。回到页面中会发现左侧多出来一列(如何放到右面的问题没有解决。。。),现在如果直接运行的话,界面是不会显示出所添加的Gridview的,因为没有绑定的数据源是不会显示的。数据源的选择也有很多,不一一列举。这里用一个DataSet作为数据源。

      先创建一个DataSet,里面有一个table。

private DataSet Data()

{

      DataSet ds=new DataSet();

      DataTable dtable=new DataTable();  //创建一个表

      DataColumn dColumn; 

      DataRow dRow;

      int i,j; 

      for(i=0;i<5;i++)

         {

              dColumn=new DataColumn("ID+i);    //创建列

              dtable.Columns.Add(dColumn);

         }

          for (j = 0; j < 5; j++)
            {
                dRow = dtable.NewRow();     //创建每一行
                dRow["ID0"] = j;
                dRow["ID1"] = j;
                dRow["ID2"] = j;
                dRow["ID3"] = j;
                dRow["ID4"] = j;
                dtable.Rows.Add(dRow);
            }

            ds.Tables.Add(dtable);
            return ds;

}

绑定数据:

 private void UpdateGridView(System.Web.UI.WebControls.GridView grdView)
        {
            DataSet ds = Data(); //  得到数据
            grdView.DataSource =ds;     // 指定数据源
            grdView.AllowPaging = true; // 允许分页
            grdView.PageSize = 2; // 每页显示2行记录
            grdView.DataBind(); // 数据绑定
         }

分页会触动以下事件发生,同时要重新绑定数据

 void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex=e.NewPageIndex;    //  设置当前页
            DataBindUpdate();               //数据要重新绑定
        }

      private void DataBindUpdate()
        {
            this.GridView1.DataSource = ds;  //ds设置为一个全局变量
            this.GridView1.AllowPaging = true;
            this.GridView1.PageSize = 2;
            this.GridView1.DataBind();
        }

开始对数据进行编辑,触动以下事件:

void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            DataBindUpdate();

        }

更新保存的数据:

void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Dictionary<string, object> dict;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            foreach (DictionaryEntry entry in e.NewValues)  //存储新值的newValues是一个DictionaryEntry类型的,构建一个Dictionary类型的dict存储
            {
                dict = new Dictionary<string, object>();
                dict.Add(entry.Key.ToString(), entry.Value.ToString());
                writeUpdataDB(dict, rowIndex);  //将更新写入到数据库中,为了使编辑的数据,在数据库中找到相应的位置,需要将当前编辑的行号记录下来
            }
            this.GridView1.EditIndex = -1;
            DataBindUpdate();

         }

 //将数据写入到数据库中:

  private void writeUpdataDB(Dictionary<string,object> dict,int index)
        {
            DataRow editrow = ds.Tables[0].Rows[index];
            editrow.BeginEdit();
            foreach(var columname in dict.Keys)
            {
                editrow[columname] = dict[columname];
            }
            editrow.EndEdit();
            editrow.AcceptChanges();
            ds.AcceptChanges();
           
        }

取消编辑:

 void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            DataBindUpdate();
        }

在Page_Load中加入以下代码:


            if (!Page.IsPostBack)
            {
                DataBindUpdate();
            }

确保数据能时时在页面上更新。

发布了27 篇原创文章 · 获赞 7 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/xiaoxiang22/article/details/8188817
今日推荐