private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { var senderGrid = (DataGridView)sender; var aa = dataGridView1.CurrentCell.RowIndex; ProductCategoryID = Convert.ToInt32(dataGridView1.Rows[aa].Cells[0].Value); if ((senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn) && (senderGrid.Columns[e.ColumnIndex].DisplayIndex == 4 && e.RowIndex >= 0) && (!(String.IsNullOrEmpty(dataGridView1.Rows[aa].Cells[0].Value.ToString())))) { txtName.Text = dataGridView1.Rows[aa].Cells[1].Value.ToString(); txtHSN.Text = dataGridView1.Rows[aa].Cells[2].Value.ToString(); txtDesc.Text = dataGridView1.Rows[aa].Cells[3].Value.ToString(); } else if ((senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn) && (senderGrid.Columns[e.ColumnIndex].DisplayIndex == 5 && e.RowIndex >= 0) && (!(String.IsNullOrEmpty(dataGridView1.Rows[aa].Cells[0].Value.ToString())))) { ProductCategoryID = Convert.ToInt32(dataGridView1.Rows[aa].Cells[0].Value); bool check = productCategoryBLL.ProductCategoryDelete(ProductCategoryID, DateTime.Now, UserId); if (check == true) { MessageBox.Show("Selected Product Category Deleted Successfully."); } else { MessageBox.Show("Selected product category cannot be deleted due to maaping with other masters."); } ProductCategoryID = 0; } FillGrid(); }