private void button2_Click(object sender, EventArgs e) { try { bool isnew = true; int vistrisua = 0; dm_vinabiz dm = new dm_vinabiz(); dm.id = ConvertType.ToInt(txt_id.Text); dm.name = txt_name.Text.Trim(); dm.path = txt_path.Text; dm.paren_id = comboBox1.SelectedValue.Equals(-1) ? (int?)null : ConvertType.ToInt(comboBox1.SelectedValue); dm.isLoai = ConvertType.ToInt(cmbLoai.SelectedValue); dm.orderid = ConvertType.ToInt(txt_orderid.Text); if (dm.id == 0) { isnew = true; SQLDatabase.AdddmVinabiz(dm); } else { isnew = false; vistrisua = dataGridView1.SelectedRows[0].Index; SQLDatabase.Updatedm_vinabiz(dm); } int vttruoc = comboBox1.SelectedIndex; BindDM_NhomVatGia(); BindDMSanPham(ConvertType.ToInt(comboBox1.SelectedValue)); comboBox1.SelectedIndex = vttruoc; 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; } } catch (Exception ex) { MessageBox.Show(ex.Message, "button2_Click"); } }