Kev*_*son 6 .net c# checkbox datagridview
我正在制作一个DataGridView带有一系列Checkboxes的水平和垂直相同的标签.任何相同的标签,复选框都将处于非活动状态,并且我只希望每个组合的两个"检查"之一有效.以下屏幕截图显示了我的内容:
DataGridView http://i46.tinypic.com/2e4m3pz.png
在下半部分检查的任何东西,我想要在鞋帮上进行UN检查.因此,如果检查[quux,spam](或[7,8]为基于零的坐标),我想要[垃圾邮件,quux]([8,7])未经检查.到目前为止我所拥有的是以下内容:
dgvSysGrid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
dgvSysGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
string[] allsysNames = { "heya", "there", "lots", "of", "names", "foo", "bar", "quux", "spam", "eggs", "bacon" };
// Add a column for each entry, and a row for each entry, and mark the "diagonals" as readonly
for (int i = 0; i < allsysNames.Length; i++)
{
dgvSysGrid.Columns.Add(new DataGridViewCheckBoxColumn(false));
dgvSysGrid.Columns[i].HeaderText = allsysNames[i];
dgvSysGrid.Rows.Add();
dgvSysGrid.Rows[i].HeaderCell.Value = allsysNames[i];
// Mark all of the "diagonals" as unable to change
DataGridViewCell curDiagonal = dgvSysGrid[i, i];
curDiagonal.ReadOnly = true;
curDiagonal.Style.BackColor = Color.Black;
curDiagonal.Style.ForeColor = Color.Black;
}
// Hook up the event handler so that we can change the "corresponding" checkboxes as needed
//dgvSysGrid.CurrentCellDirtyStateChanged += new EventHandler(dgvSysGrid_CurrentCellDirtyStateChanged);
dgvSysGrid.CellValueChanged += new DataGridViewCellEventHandler(dgvSysGrid_CellValueChanged);
}
void dgvSysGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
Point cur = new Point(e.ColumnIndex, e.RowIndex);
// Change the diagonal checkbox to the opposite state
DataGridViewCheckBoxCell curCell = (DataGridViewCheckBoxCell)dgvSysGrid[cur.X, cur.Y];
DataGridViewCheckBoxCell diagCell = (DataGridViewCheckBoxCell)dgvSysGrid[cur.Y, cur.X];
if ((bool)(curCell.Value) == true)
{
diagCell.Value = false;
}
else
{
diagCell.Value = true;
}
}
/// <summary>
/// Change the corresponding checkbox to the opposite state of the current one
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void dgvSysGrid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
Point cur = dgvSysGrid.CurrentCellAddress;
// Change the diagonal checkbox to the opposite state
DataGridViewCheckBoxCell curCell = (DataGridViewCheckBoxCell)dgvSysGrid[cur.X, cur.Y];
DataGridViewCheckBoxCell diagCell = (DataGridViewCheckBoxCell)dgvSysGrid[cur.Y, cur.X];
if ((bool)(curCell.Value) == true)
{
diagCell.Value = false;
}
else
{
diagCell.Value = true;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果我使用CellValueChanged事件,那么单元格值的变化似乎总是"落后"你实际点击的地方,如果我处于"脏"状态,我不知道如何获取当前单元格curCell以null形式出现(暗示当前的单元格地址在某种程度上是错误的,但我没有尝试将该值取出)意味着路径根本不起作用.
基本上,如何使用正确的布尔值获取"正确"的地址,以便我的翻转算法可以工作?
Kev*_*son 16
最终,正是这样做的CurrentCellDirtyStateChanged事件,但你需要以正确的方式做到这一点.正确的方式是MSDN,虽然乍一看没有意义.
上面的一个片段,我最终做的是下面的内容:
// Hook up the event handler so that we can change the "corresponding" checkboxes as needed
dgvSysGrid.CurrentCellDirtyStateChanged += new EventHandler(dgvSysGrid_CurrentCellDirtyStateChanged);
dgvSysGrid.CellValueChanged += new DataGridViewCellEventHandler(dgvSysGrid_CellValueChanged);
}
void dgvSysGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
Point cur = new Point(e.ColumnIndex, e.RowIndex);
// Change the diagonal checkbox to the opposite state
DataGridViewCheckBoxCell curCell = (DataGridViewCheckBoxCell)dgvSysGrid[cur.X, cur.Y];
DataGridViewCheckBoxCell diagCell = (DataGridViewCheckBoxCell)dgvSysGrid[cur.Y, cur.X];
if ((bool)(curCell.Value) == true)
{
diagCell.Value = false;
}
else
{
diagCell.Value = true;
}
}
void dgvSysGrid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvSysGrid.IsCurrentCellDirty)
{
dgvSysGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,所发生的一切都是CurrentCellDirtyStateChanged事件触发CellValueChanged事件,就是这样.如果您只是附加CellValueChanged事件,那么它只会在您离开单元格后触发.我不知道原因究竟是什么(考虑到它是一个复选框,是不是立刻"完成"了?),但这就是发生的事情.并且上面的代码工作,因为复选框在单击时更改为RIGHT.所以它有效.
| 归档时间: |
|
| 查看次数: |
22565 次 |
| 最近记录: |