Как войти в событие cellenter при нажатии флажка datagridview?
i have three events in cellcontentclick and cellenter and cellleave events of datagridview..winforms... iam trying to write code this is done :when my checkbox is clicked it allows us to enter data in othercolumn.when i uncheck it it disables us to enter data in other column. ths is what i have to do : when checkbox cell is clicked the cursor has to go to the particular cell automatically. and must allow us to write data in it..while leaving the cell using cellleave event i have to check whether is empty or not.if empty that checkbox must be uncheked that means have to make it false.and must not allow any user to enter data in other column.else if cell while leaving is not empty it has to store the data of that row in database
Что я уже пробовал:
private void button1_Click(object sender, EventArgs e) { int row = 0; dataGridView1.Rows.Add(); row = dataGridView1.Rows.Count - 1; dataGridView1["AMENITIESNAME", row].Value = textBox1.Text; dataGridView1["DESCRIPTION", row].Value = textBox2.Text; dataGridView1["CHECKBOX", row].Value = false; dataGridView1["AMOUNT", row].ReadOnly = true; LoadSerial(); } private async void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e) { if (lastCell != null) { var lastAmtCell = dataGridView1.Rows[lastCell.RowIndex].Cells["AMOUNT"]; if (lastAmtCell.ReadOnly == true) { // MessageBox.Show("working"); lastCell.Value = false; } else { // MessageBox.Show("working"); lastCell.Value = true; } } // System.Threading.Thread.Sleep(300); // Stopwatch stopwatch = Stopwatch.StartNew(); // Thread.SpinWait(1000000); // await Task.Delay(20000); var amountCell = dataGridView1.Rows[e.RowIndex].Cells["AMOUNT"]; if (e.RowIndex > -1 && dataGridView1.CurrentCell == dataGridView1.Rows[e.RowIndex].Cells["CHECKBOX"]) { if (amountCell.ReadOnly == true) { amountCell.ReadOnly = false; amountCell.Style.BackColor = Color.White; } else { dataGridView1.Rows[e.RowIndex].Cells["AMOUNT"].ReadOnly = true; amountCell.Style.BackColor = Color.Gray; //dataGridView1.Columns["AMOUNT"].Visible = true; } lastCell = dataGridView1.CurrentCell; } } private void dataGridView1_CellValidating_1(object sender, DataGridViewCellValidatingEventArgs e) { /* foreach (DataGridViewRow row in dataGridView1.Rows) { //dataGridView1.Rows[e.RowIndex].Cells["CHECKBOX"].Value=true; //dataGridView1.Rows[e.RowIndex].Cells["CHECKBOX"].Value = true; DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[3]; if (chk.Value.ToString().ToLower() == " false") { //dataGridView1.CurrentCell = dataGridView1[0, 2]; if (Convert.ToString(row.Cells["AMOUNT"].Value) != string.Empty) { SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-T09HLQF;Initial Catalog=BANQUET HALL ;PASSWORD=Secret;Integrated Security=True;"); con.Open(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { //string StrQuery = @"INSERT INTO AMENITIES VALUES (" + dataGridView1.Rows[i].Cells[0].Value + ", " + dataGridView1.Rows[i].Cells[1].Value + "," + dataGridView1.Rows[i].Cells[2].Value + "," + dataGridView1.Rows[i].Cells[3].Value + "," + dataGridView1.Rows[i].Cells[4].Value + ");"; string StrQuery = @"INSERT INTO AMENITIES(ID,AMENITIESNAME,DESCRIPTION,CHECKBOX,AMOUNT) VALUES (@ID,@AMENITIESNAME,@DESCRIPTION,@CHECKBOX,@AMOUNT)"; SqlCommand CMD = new SqlCommand(StrQuery, con); CMD.Parameters.AddWithValue("@ID", dataGridView1.Rows[i].Cells[0].Value); CMD.Parameters.AddWithValue("@AMENITIESNAME", dataGridView1.Rows[i].Cells[1].Value); CMD.Parameters.AddWithValue("@DESCRIPTION", dataGridView1.Rows[i].Cells[2].Value); CMD.Parameters.AddWithValue("@CHECKBOX", dataGridView1.Rows[i].Cells[3].Value); CMD.Parameters.AddWithValue("@AMOUNT", dataGridView1.Rows[i].Cells[4].Value); CMD.ExecuteNonQuery(); // dataGridView1.Rows[lastCell.RowIndex].Cells["AMOUNT"].ReadOnly = true; } chk.Value = "false"; //dataGridView1.Rows[lastCell.RowIndex].Cells["AMOUNT"].ReadOnly = true; } else { MessageBox.Show("please provide amount"); return; // dataGridView1.Rows[e.RowIndex].Cells["CHECKBOX"].Value = false; } } }*/ } private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e) { } private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 4) { foreach (DataGridViewRow row in dataGridView1.Rows) { if (Convert.ToString(row.Cells["AMOUNT"].Value) == string.Empty) { MessageBox.Show("Please enter value"); dataGridView1.Rows[e.RowIndex].Cells["CHECKBOX"].Value = false; dataGridView1.Rows[e.RowIndex].Cells[4].Value = null; return; } else { SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-T09HLQF;Initial Catalog=BANQUET HALL ;PASSWORD=Secret;Integrated Security=True;"); con.Open(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { //string StrQuery = @"INSERT INTO AMENITIES VALUES (" + dataGridView1.Rows[i].Cells[0].Value + ", " + dataGridView1.Rows[i].Cells[1].Value + "," + dataGridView1.Rows[i].Cells[2].Value + "," + dataGridView1.Rows[i].Cells[3].Value + "," + dataGridView1.Rows[i].Cells[4].Value + ");"; string StrQuery = @"INSERT INTO AMENITIES(ID,AMENITIESNAME,DESCRIPTION,CHECKBOX,AMOUNT) VALUES (@ID,@AMENITIESNAME,@DESCRIPTION,@CHECKBOX,@AMOUNT)"; SqlCommand CMD = new SqlCommand(StrQuery, con); CMD.Parameters.AddWithValue("@ID", dataGridView1.Rows[i].Cells[0].Value); CMD.Parameters.AddWithValue("@AMENITIESNAME", dataGridView1.Rows[i].Cells[1].Value); CMD.Parameters.AddWithValue("@DESCRIPTION", dataGridView1.Rows[i].Cells[2].Value); CMD.Parameters.AddWithValue("@CHECKBOX", dataGridView1.Rows[i].Cells[3].Value); CMD.Parameters.AddWithValue("@AMOUNT", dataGridView1.Rows[i].Cells[4].Value); CMD.ExecuteNonQuery(); // dataGridView1.Rows[lastCell.RowIndex].Cells["AMOUNT"].ReadOnly = true; } } } } } } }