天天看點

winform DataGridView輕按兩下修改單元格的值

定義全局變量      

string abcmycode = "";//目前行自編号

        bool tf = false;//是否輕按兩下

   //輕按兩下可編輯

        private void dgvOutClassVerify_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {

            if (dgvOutClassVerify.Rows.Count > 0) {

                abcmycode = dgvOutClassVerify.CurrentRow.Cells["MainTop_Mycode"].Value.ToString();              

                dgvOutClassVerify.CurrentRow.Cells["MainTop_Mycode"].ReadOnly = false;

                tf = true;

            }

        }

        private void dgvOutClassVerify_CellValidated(object sender, DataGridViewCellEventArgs e) {

            try {

                if (tf == true) {

                    tf = false;

                    if (dgvOutClassVerify.Rows.Count > 0) {

                        int abcid = Convert.ToInt32(dgvOutClassVerify.CurrentRow.Cells["MainTop_ID"].Value.ToString());

                        string mycodes = dgvOutClassVerify.CurrentRow.Cells["MainTop_Mycode"].Value.ToString();

                        if (abcmycode != mycodes) {

                            if (mycodes.Equals("")) {

                                PublicClass.ShowMessage("自編号不能為空!");

                                dgvOutClassVerify.CurrentRow.Cells["MainTop_Mycode"].Value = abcmycode;

                                return;

                            }

                            //驗證語句

                               //----------------------------

                            //更新語句

                            string cbasql = "update T_OutIn_MainTop set MainTop_MyCode='" + mycodes + "' where MainTop_ID= " + abcid;

                            int nn = DataAccess.ExecuteNonQuery(cbasql, false, null);

                            if (nn > 0) {

                                dgvOutClassVerify.CurrentRow.Cells["MainTop_MyCode"].ReadOnly = true;

                                PublicClass.ShowMessage("自編号修改成功!");

                                return;

                            }

                        }

                    }

                }

            } catch (Exception ex) {

                PublicClass.ShowMessage(ex.Message);

                return;

            }

        }

繼續閱讀