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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章