是否有可能添加功能「檢查所有」來的WinForms 的DataGridView的DataGridViewCheckBoxColumn?DataGridView中自定義列標題的內容(CheckBox控件)
看起來應該像下面這樣:
點擊白標明的複選框應檢查/取消選中所有複選框在網格中。
正如我所見,列標題只能包含字符串值。有什麼解決方法嗎?
是否有可能添加功能「檢查所有」來的WinForms 的DataGridView的DataGridViewCheckBoxColumn?DataGridView中自定義列標題的內容(CheckBox控件)
看起來應該像下面這樣:
點擊白標明的複選框應檢查/取消選中所有複選框在網格中。
正如我所見,列標題只能包含字符串值。有什麼解決方法嗎?
最終實現主要是解決方案,在this文章薩米爾建議。
但是當網格水平滾動條移動時,它需要修復複選框的位置。因此,下面是需要更改的方法:
private void frmSelectAll_Load(object sender, EventArgs e)
{
AddHeaderCheckBox();
HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);
HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
dgvSelectAll.CellValueChanged += new DataGridViewCellEventHandler(dgvSelectAll_CellValueChanged);
dgvSelectAll.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
dgvSelectAll.CellPainting += new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting);
BindGridView();
var checkboxHeaderCellRect = dgvSelectAll.GetCellDisplayRectangle(0, -1, false);
headerCheckboxRightMargin = (checkboxHeaderCellRect.Width - HeaderCheckBox.Width)/2;
}
private int headerCheckboxRightMargin;
private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
{
//Get the column header cell bounds
Rectangle oRectangle = this.dgvSelectAll.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);
Point oPoint = new Point();
oPoint.X = oRectangle.Location.X + (oRectangle.Width - headerCheckboxRightMargin - HeaderCheckBox.Width);
oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height)/2 + 1;
if (oPoint.X < oRectangle.X)
{
HeaderCheckBox.Visible = false;
}
else
{
HeaderCheckBox.Visible = true;
}
//Change the location of the CheckBox to make it stay on the header
HeaderCheckBox.Location = oPoint;
}
謝謝。但是這篇文章在水平滾動網格方面效果不佳。我發現了另一個,基於您的鏈接:http://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat。儘管它也需要修復 –