private void button2_Click(object sender, EventArgs e) { int vistrisua = 0; bool isnew = false; if (!radioButton1.Checked) { if (cmb_nhomhs.SelectedValue.ToString() == "-1") { MessageBox.Show("Vui lòng chọn nhóm hồ sơ", "Thông Báo"); return; } if (txtMaid.Text == "") { MessageBox.Show("Vui lòng Mã Ngành", "Thông Báo"); txtMaid.Focus(); return; } } dm_hsct dm = new dm_hsct(); dm.id = ConvertType.ToInt(txt_id.Text); dm.name = txt_name.Text; dm.path = txt_path.Text; dm.maid = txtMaid.Text; if (cmb_nhomhs.SelectedValue.ToString() != "-1") { dm.parenid = ConvertType.ToInt(cmb_nhomhs.SelectedValue); } dm.orderid = ConvertType.ToInt(txt_orderid.Text); dm.capid = ConvertType.ToInt(cmd_cap.SelectedValue); if (dm.id == 0) { isnew = true; if (ConvertType.ToInt(SQLDatabase.ExcScalar(string.Format("select count(*) as soluong from dm_hsct where maid='{0}'", dm.maid))) == 0) { SQLDatabase.Adddm_hsct(dm); } else { MessageBox.Show("Vui lòng chọn mã khác, Mã đã tồn tại", "Thông Báo"); txtMaid.Focus(); txtMaid.SelectAll(); } } else { isnew = false; vistrisua = dataGridView1.SelectedRows[0].Index; string str = string.Format("select count(*) as soluong from dm_hsct where id<>'{0}' and maid='{1}'", dm.id, dm.maid); if (ConvertType.ToInt(str) > 0) { MessageBox.Show("Vui lòng chọn mã khác, Mã đã tồn tại", "Thông Báo"); txtMaid.Focus(); txtMaid.SelectAll(); } else { SQLDatabase.Updatedm_hsct(dm); } } BindDMNhom(); BindDanhmuc(); dataGridView1.ClearSelection(); if (isnew) { int nRowIndex = dataGridView1.Rows.Count - 1; if (dataGridView1.Rows.Count - 1 >= nRowIndex) { dataGridView1.FirstDisplayedScrollingRowIndex = nRowIndex; dataGridView1.Rows[nRowIndex].Selected = true; dataGridView1.Rows[nRowIndex].Cells[0].Selected = true; } } else { dataGridView1.Rows[vistrisua].Selected = true; } }