GridView提示.txt

來源:互聯網
上載者:User

ExtractValuesFromCell 方法使用:而該方法也被很多種列類型所支援
DataControlField, BoundField, AutoGeneratedField, CheckBoxField, ImageField, TemplateField, DynamicField,你可以在 GridView 的 RowUpdating, RowDeleting 等事件中使用它。利用該方法,可以將值提取到所需的字典裡去,然後再從字典中讀取。這些字典包括:e.Keys, e.NewValues, e.OldValues 等。

代碼如下:

 

Code
  GridViewRow row =this.GridView1.Rows[e.RowIndex];     
        //primary key 
         this.GridView1.Columns[0].ExtractValuesFromCell(e.Keys, row.Cells[0] as DataControlFieldCell, DataControlRowState.Edit, true);
        //Update columns
        this.GridView1.Columns[1].ExtractValuesFromCell(e.NewValues, row.Cells[1] as DataControlFieldCell, DataControlRowState.Edit, true);
        //get primary key
        string id = e.Keys["userName"].ToString();
        //get Update columns
        string userTel = e.NewValues["userTel"].ToString();
        // 執行相關的資料庫更新操作
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
        {
            con.Open();
            using (SqlCommand com = new SqlCommand("update users set userTel='"+userTel+"' where userName='"+id+"'", con))
            {
                com.ExecuteNonQuery();
            }
        }
        

這樣,在大多數場合我們可以儘可能多的使用 BoundField,並且也能正確讀取到其編輯時的值,省下自訂 TemplateField 的一堆代碼了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.