/// <summary> /// dgvSubDt_DataBindingComplete /// </summary> /// <param name="dgv"></param> /// <param name="isOpt"></param> public static void DataGridViewCellContentCheck(System.Windows.Forms.DataGridView dgv, System.Windows.Forms.DataGridViewCellEventArgs e) { try { int rowIndex = e.RowIndex; if (rowIndex == -1) { return; } string headerName = dgv.Columns[dgv.CurrentCell.ColumnIndex].Name; if (headerName != "Opt") { return; } if (dgv.Rows.Count <= 0) { return; } string selectValue = dgv.Rows[rowIndex].Cells["Opt"].EditedFormattedValue.ToString(); dgv.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Turquoise; //背景绿色 if (selectValue == "True") { for (int j = 0; j < dgv.Rows.Count; j++) { if (string.Equals(dgv.Rows[j].Cells["Opt"].EditedFormattedValue.ToString(), "True")) { if (!int.Equals(rowIndex, j)) //行号不相等 { dgv.EndEdit(); //去掉勾选 System.Windows.Forms.DataGridViewCheckBoxCell checkCell = (System.Windows.Forms.DataGridViewCheckBoxCell)dgv.Rows[j].Cells["Opt"]; Boolean flag = Convert.ToBoolean(checkCell.Value); if (flag) { checkCell.Value = false; } string colorFlag = string.Empty; if (dgv.Columns.Contains("ColorFlag")) { colorFlag = dgv.Rows[j].Cells["ColorFlag"].EditedFormattedValue.ToString(); } switch (colorFlag) { case "green": dgv.Rows[j].DefaultCellStyle.BackColor = System.Drawing.Color.MediumAquamarine; break; case "red": dgv.Rows[j].DefaultCellStyle.BackColor = System.Drawing.Color.LightCoral; break; case "yellow": dgv.Rows[j].DefaultCellStyle.BackColor = System.Drawing.Color.Wheat; break; default: dgv.Rows[j].DefaultCellStyle.BackColor = System.Drawing.Color.Empty; break; } dgv.EndEdit(); //return; } } } //Util.DataGridViewHelper.DataGridViewDataBindingComplete(dgv); } } catch (Exception ex) { string logMessage = string.Format("DataGridViewCellContentCheck 勾选操作失败,原因:{0}", ex.Message); Log4netUtil.Log4NetHelper.Info(logMessage, @"Exception"); } }
private void UnleashedData_CellValueChanged(object sender, System.Windows.Forms.DataGridViewCellEventArgs e) { if (e.ColumnIndex == 4 && e.RowIndex != -1) { // Handle checkbox state change here if (UnleashedData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True") { //do something string supplier = UnleashedData.Rows[e.RowIndex].Cells[6].Value.ToString(); string productgroup = UnleashedData.Rows[e.RowIndex].Cells[14].Value.ToString(); if (glbSupplier == string.Empty || glbSupplier == supplier) { glbSupplier = supplier; } else { MessageBox.Show("Please choose same supplier.", "Information", MessageBoxButton.OK, MessageBoxImage.Information); UnleashedData.Rows[e.RowIndex].Cells[4].Value = false; UnleashedData.CancelEdit(); } if (glbProduct == string.Empty || glbProduct == productgroup) { switch (productgroup) { case "Meters": glbWarehouse = "MWF"; break; case "Sensors": glbWarehouse = "MWQ"; break; case "Projects": glbWarehouse = "Projects"; break; default: break; } glbProduct = productgroup; } else { MessageBox.Show("Please choose same product group.", "Information", MessageBoxButton.OK, MessageBoxImage.Information); UnleashedData.Rows[e.RowIndex].Cells[4].Value = false; UnleashedData.CancelEdit(); } } else { //do something int encountercheck = 0; foreach (System.Windows.Forms.DataGridViewRow row in UnleashedData.Rows) { System.Windows.Forms.DataGridViewCheckBoxCell chk = (System.Windows.Forms.DataGridViewCheckBoxCell)row.Cells[4]; if (chk.Value != null) { bool checkTrue = (bool)chk.Value; //because chk.Value is initialy null if (checkTrue) { encountercheck = 1; } } } if (encountercheck == 0) { glbSupplier = string.Empty; glbProduct = string.Empty; } } } }