private void btnAddRangeCheck_Click(object sender, EventArgs e) { try { DataGridViewSelectedRowCollection selectedRows = dgMain.SelectedRows; if (selectedRows.Count == 0) { MessageBox.Show("请选择要添加范围验证的行!"); } else { String field = ConvertObjToString(selectedRows[0].Cells[1].Value); Boolean checkRange = ConvertObjToString(selectedRows[0].Cells[6].Value).ToLower().Equals("true"); if (checkRange) { MessageBox.Show("当前选中行验证类型为[验证范围],不能再添加,请选择其他行!"); return; } Int32 index = Int32.Parse(selectedRows[0].Cells[0].Value.ToString()); DataRow[] selectedDTRow = (dgMain.DataSource as DataTable).Select("NO = " + index); if (selectedDTRow.Length == 0) { MessageBox.Show(""); } InputRangeForm inputRangeForm = new InputRangeForm(field); DialogResult result = inputRangeForm.ShowDialog(); if (result.Equals(DialogResult.Yes)) { DataTable tempDT = dgMain.DataSource as DataTable; DataRow newRow = tempDT.NewRow(); newRow.ItemArray = selectedDTRow[0].ItemArray; newRow["CheckRange"] = true; newRow["CheckNull"] = false; newRow["CheckEmpty"] = false; newRow["Range"] = inputRangeForm.Range; newRow["NO"] = index; tempDT.Rows.Add(newRow); DataView dataView = tempDT.DefaultView; dataView.Sort = "NO asc"; tempDT = dataView.ToTable(); for (int i = 0; i < tempDT.Rows.Count; i++) { DataRow tempRow = tempDT.Rows[i]; tempRow["NO"] = i + 1; } dgMain.DataSource = tempDT; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void dgMain_CellContentClick(object sender, DataGridViewCellEventArgs e) { try { int rowIndex = e.RowIndex; int colIndex = e.ColumnIndex; DataGridViewColumn column = dgMain.Columns[colIndex]; if (column.Name.Equals("CheckRange")) { String field = ConvertObjToString(dgMain.Rows[rowIndex].Cells[1].Value); Boolean checkRange = !ConvertObjToString(dgMain.Rows[rowIndex].Cells[colIndex].Value).ToLower().Equals("true"); if (checkRange) { InputRangeForm inputRangeForm = new InputRangeForm(field); DialogResult result = inputRangeForm.ShowDialog(); if (result.Equals(DialogResult.Yes)) { DataTable tempDT = dgMain.DataSource as DataTable; DataRow[] rows = tempDT.Select("Field = '" + field + "'"); if (rows.Length > 0) { rows[0]["CheckRange"] = checkRange; rows[0]["CheckNull"] = false; rows[0]["CheckEmpty"] = false; rows[0]["Range"] = inputRangeForm.Range; dgMain.DataSource = tempDT; } } } } else if (column.Name.Equals("CheckNull") || column.Name.Equals("CheckEmpty")) { String field = ConvertObjToString(dgMain.Rows[rowIndex].Cells[1].Value); Boolean check = !ConvertObjToString(dgMain.Rows[rowIndex].Cells[column.Name].Value).ToLower().Equals("true"); DataTable tempDT = dgMain.DataSource as DataTable; DataRow[] rows = tempDT.Select("Field = '" + field + "'"); if (rows.Length > 0) { String otherColName = column.Name.Equals("CheckNull") ? "CheckEmpty" : "CheckNull"; rows[0][column.Name] = check; rows[0][otherColName] = !check; rows[0]["CheckRange"] = false; dgMain.DataSource = tempDT; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }