ASP.NET DataGrid的多行提交

來源:互聯網
上載者:User
www.chinacs.net  
儘管ASP.NET DataGrid是眾所周知非常好的表格控制項,不過,提起DataGrid的編輯功能,我們卻不敢恭維了,就拿DataGrid的資料提交功能來說,的確存在很大的問題:在DataGrid中,每編輯一行就要提交一行,即所謂“單行編輯、單行提交”,這樣的話,如果編輯的行數過多,不僅使用者操作繁瑣,還會造成對伺服器的頻繁訪問,極大降低系統效率。

 當然了,有一種借屍還魂的解決方案,那就是把所要編輯的內容轉到其他的頁中在TextBox中進行編輯。不過,仔細想想,這種方法難道不是自己在騙自己嗎,還有在Grid中我們編輯的時候總不能老是用Tab鍵來實現Grid(TextBox)之間的跳轉吧,如果響應斷行符號事件,那麼需要程式員浪費很大的精力來開發。

 如何解決上述問題呢?下面我向大家推薦一個我正在使用的國產DataGrid:SmartGrid(天空軟體站可以下載:http://www.skycn.com/soft/23547.html ),這個控制項我已經用了好長的時間了,現在來同大家探討一下SmartGrid的多行提交的方法:SmartGrid並沒有DataGrid中的那些按鈕列而是整個的表單只有一個提交按鈕,無論你更改了一行或者是多行都可以一次性的提交,下面來隨便看點例子:

執行個體 代碼:

修改按鈕的代碼:
private void btonSave_Click(object sender, System.EventArgs e)

         {

              this.DataGrid1.ReadOnly = false;//進入編輯

              this.DataGrid1.AllowAdd = true;//允許添加

              this.DataGrid1.AllowDelete = true;//允許刪除

         }

此段代碼是smartgrid的專屬的屬性你可以設添加刪除 編輯 的各種的功能

儲存按鈕的代碼:
private void Button2_Click(object sender, System.EventArgs e)

         {

              DataTable t = (DataTable)this.SmartGrid1.DataSource;

 

              this.sqlDataAdapter1.Update(t);

             

              t.Clear();

 

              this.sqlDataAdapter1.Fill(t);

 

              this.SmartGrid1.DataSource = t;

         }

這是整體的把資料提交到資料庫中,這種做法適合大資料量的情況

還有一種是資料逐行的提交到伺服器:

代碼:

private void btonSave_Click(object sender, System.EventArgs e)

         {            

              DataTable tb=(DataTable)this.SmartGrid1.DataSource;

              SqlParameter[] parameters=new SqlParameter[5];

              foreach(DataRow dr in tb.Rows)

              {

                   parameters[0]=new SqlParameter("@customerId",""+dr[1]+"");

                   parameters[1]=new SqlParameter("@companyName",""+dr[0]+"");

                   parameters[2]=new SqlParameter("@contactName",""+dr[2]+"");

                   parameters[3]=new SqlParameter("@contactTitle",""+dr[3]+"");

                   parameters[4]=new SqlParameter("@address",""+dr[4]+"");

                   //EamPd 是類Execute是執行預存程序的函數parameters是預存程序所需要的參數

                   EamPd.Execute("CreatLayer",parameters);

              }            

         }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.