我正在製作一個DataGridView
,其中包含一系列複選框,並在水平和垂直方向使用相同的標籤。任何標籤都是相同的,複選框將處於非活動狀態,我只希望每個組合的兩個「檢查」中的一個有效。下面的屏幕截圖顯示了我擁有的內容: DataGridView http://i46.tinypic.com/2e4m3pz.pngDataGridView複選框事件
任何在下半部分檢查的東西,我都需要在上面檢查聯合國檢查。因此,如果選擇[quux,spam](或[7,8]用於從零開始的座標),我想[spam,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;
}
}
這個問題來的是,單元格的值改似乎總是「一個落後」,您實際點擊,如果我使用CellValueChanged
事件,我不知道如果我處於「髒」狀態,因爲curCell作爲空值(表明當前單元格地址錯誤,但我沒有嘗試將該值取出),如何獲取當前單元格,這意味着該路徑不是「根本不工作。
基本上,我如何獲得正確的布爾值的「正確的」地址,以便我的翻轉算法將工作?
是的! ......我的搜尋結束了......我現在可以過得幸福快樂。謝謝。 – zmaster 2012-12-11 09:29:39