|
■No87482 (MTK さん) に返信 > 一番上のDataGridViewの1行目はチェックボックスセルになっており > そのセルをチェックした時に、チェックが入った列(縦に並ぶ3つのDataGridViewを含む)を > 入力不可になるように設定をしています。
その場合、チェックボックスセルは入力不可にしないのですよね。
using System; using System.Drawing; using System.Windows.Forms;
public partial class Form1 : Form { private DataGridView dgvTop, dgvMiddle, dgvBottom; private readonly Color OddColorNormal = Color.PaleGoldenrod; private readonly Color EvenColorNormal = Color.PowderBlue; private readonly Color OddColorLocked = Color.Gainsboro; private readonly Color EvenColorLocked = Color.DarkGray; private readonly Color[] BackgroundColors;
private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // AlternatingRowsDefaultCellStyle に頼らず、自前で切り替え
if (e.ColumnIndex < 0 || e.RowIndex < 0) { return; } if (sender == dgvTop && e.RowIndex == 0) { return; } int index = (bool)dgvTop[e.ColumnIndex, 0].Value ? 2 : 0; if (sender == dgvTop) { index += (e.RowIndex % 2 == 1) ? 0 : 1; } else { index += (e.RowIndex % 2 == 0) ? 0 : 1; } e.CellStyle.BackColor = BackgroundColors[index]; }
public Form1() { InitializeComponent(); BackgroundColors = new Color[] { OddColorNormal, EvenColorNormal, OddColorLocked, EvenColorLocked }; Controls.Clear(); Controls.Add(dgvMiddle = new DataGridView() { Dock = DockStyle.Fill, TabIndex = 1 }); Controls.Add(dgvTop = new DataGridView() { Dock = DockStyle.Top, TabIndex = 0 }); Controls.Add(dgvBottom = new DataGridView() { Dock = DockStyle.Bottom, TabIndex = 2 });
dgvTop.CellFormatting += dgv_CellFormatting; dgvMiddle.CellFormatting += dgv_CellFormatting; dgvBottom.CellFormatting += dgv_CellFormatting;
dgvTop.ColumnCount = dgvMiddle.ColumnCount = dgvBottom.ColumnCount = 8; dgvTop.RowCount = dgvMiddle.RowCount = dgvBottom.RowCount = 50; for (int c = 0; c < dgvTop.ColumnCount; c++) { dgvTop[c, 0] = new DataGridViewCheckBoxCell(false) { TrueValue = true, FalseValue = false, Value = false, Style = { Alignment = DataGridViewContentAlignment.MiddleCenter} }; } dgvTop.ColumnHeadersVisible = false; dgvTop.Rows[0].DefaultCellStyle = new DataGridViewCellStyle() { BackColor = SystemColors.Control, SelectionBackColor = SystemColors.Control }; dgvTop.CurrentCell = dgvTop[0, 1]; dgvTop.CurrentCellDirtyStateChanged += (sender, e) => { var cell = dgvTop.CurrentCell as DataGridViewCheckBoxCell; if (cell == null) { return; } var dgv = (DataGridView)sender; if (dgv.IsCurrentCellDirty) { dgv.CommitEdit(DataGridViewDataErrorContexts.Commit); }
bool isReadOnly = (cell.TrueValue == cell.Value); int columnIndex = cell.ColumnIndex; for (int r = 1 ; r < dgvTop.RowCount; r++) { dgvTop[columnIndex, r].ReadOnly = isReadOnly; } for (int r = 0; r < dgvMiddle.RowCount; r++) { dgvMiddle[columnIndex, r].ReadOnly = isReadOnly; } for (int r = 0; r < dgvBottom.RowCount; r++) { dgvBottom[columnIndex, r].ReadOnly = isReadOnly; } dgvTop.InvalidateColumn(columnIndex); dgvMiddle.InvalidateColumn(columnIndex); dgvBottom.InvalidateColumn(columnIndex); }; } }
|