如何用自定义标题替换 DataGridView 的 HeaderCells? [英] How to replace the HeaderCells of a DataGridView with custom headers?
问题描述
何时将 DataGridViewColumnHeaderCell
替换为自定义 HeaderCell
(此处:CustomDataGridViewColumnHeaderCell
),当 DataSource
一个DataGridView已经被设置为一个DataTable
对象并且还保留了之前HeaderCell的文本,它被设置为DataTable对应Column的名称?
我有一个用于 DataGridView
的自定义标头类:
公共类 CustomDataGridViewColumnHeaderCell : DataGridViewColumnHeaderCell{//在头部添加一个按钮}
A DataTable
被设置为 DataSource
,自动创建列.如何用自定义标题单元格替换默认标题单元格(上述实例)?
更多细节
要解决的问题是,DataGridView
需要在其标题单元格中嵌入可点击的按钮,在上面的屏幕截图中箭头指向的位置.为此,有一个 CustomDataGridViewColumnHeaderCell
类.为了在 DataGridView
中显示数据,一个 DataTable
被指定为它的数据源.例如:
myDataGridView.DataSource = myDataTable;
执行此操作后,DataGridView
将具有与 DataTable
中的列相对应的列.我从默认标题类派生了一个类,以便按钮出现在列标题内.
使用 System.Drawing;使用 System.Windows.Forms;类 DGVButtonHeaderCell : DataGridViewColumnHeaderCell{公共只读按钮按钮;私人填充 m_ButtonPadding = Padding.Empty;公共 DGVButtonHeaderCell(int buttonWidth = 40){button = new Button() { BackColor = Color.Orange, Width = buttonWidth };m_ButtonPadding = new Padding(button.Width + 2, 0, 0, 0);button.Click += ButtonClick;}public void ReplaceHeaderCell(DataGridViewColumnHeaderCell previousHeader){if (previousHeader == null) 返回;SetStandardValues(previousHeader);var dgv = previousHeader.DataGridView;dgv.Columns[previousHeader.OwningColumn.Index].HeaderCell = this;previousHeader.Dispose();}私有无效 SetStandardValues(DataGridViewColumnHeaderCell 前一个){if (previous == null) 返回;this.Style = previous.Style;button.Font = this.Style.Font ??previous.DataGridView.ColumnHeadersDefaultCellStyle.Font;//等等.}private void ButtonClick(object sender, EventArgs e){OnClick(new DataGridViewCellEventArgs(ColumnIndex, RowIndex));MessageBox.Show(你点击了我");}protected override void OnDataGridViewChanged(){如果(this.DataGridView == null)返回;this.DataGridView.Controls.Add(button);}protected override void Paint(Graphics g, Rectangle clipBounds, Rectangle bounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advBorderStyle, DataGridViewPaintParts 部分){cellStyle.Padding = Padding.Add(m_ButtonPadding, cellStyle.Padding);base.Paint(g,clipBounds,bounds,rowIndex,elementState,value,formattedValue,errorText,cellStyle,advBorderStyle,parts);button.Location = new Point(bounds.Left, bounds.Top + 2);button.Height = bounds.Height - 4;}受保护的覆盖无效处置(布尔处置){如果(处置){button.MouseClick -= ButtonClick;button.Dispose();}base.Dispose(处置);}}
When to replace a DataGridViewColumnHeaderCell
with a custom HeaderCell
(here: CustomDataGridViewColumnHeaderCell
), when the DataSource
of a DataGridView is already set to a DataTable
object and also preserve the text of the previous HeaderCell, which is set to the name of the corresponding Column of the DataTable?
I have a custom header class for a DataGridView
:
public class CustomDataGridViewColumnHeaderCell : DataGridViewColumnHeaderCell
{
// adds a button to the header
}
A DataTable
is being set as the DataSource
, the Columns are created automatically. How can I replace the default header cells with custom header cells (instances of the above)?
More detail
The problem to solve is, a DataGridView
needs to have clickable buttons embedded in its header cells, where the arrow is pointing to in the screenshot above. For that, there is a class CustomDataGridViewColumnHeaderCell
. To show data in the DataGridView
, a DataTable
is being assigned as its data source. For example:
myDataGridView.DataSource = myDataTable;
After doing this, the DataGridView
will have columns corresponding to those in the DataTable
. I have derived a class from the default header classes, so that the buttons appear inside the column headers.
The HeaderCell of a DataGridViewColumn can be replaced at any time.
Even when the DataSource of the DataGridView is already set: in this case, you may want to set the Style of the current HeaderCell
to your new cell, in case some values need to be replicated (when the Column are auto-generated, there's probably not much to copy, but since the Style property can be set, let's set it).
In the example, I'm adding a ReplaceHeaderCell()
method to the custom HeaderCell
, which receives the instance of the current header, copies some properties then disposes of it before replacing the corresponding Column's HeaderCell
with itself.
If the Button we're adding to the HeaderCell
is a standard Button Control, this Control must be added to the DataGridView.Controls
collection (so it will be visible and brought to front).
If the Button is painted, overriding the Paint method, of course there's nothing to parent :).
Note: the Button is added in the OnDataGridViewChanged()
method override. This method is called each time a DataGridView object becomes the owner of the HeaderCell. When the Parent DataGridView changes, the Button is automatically added to its Controls collection.
► Here, the Paint method is overridden anyway: it's used to calculate the HeaderCell Text padding, so the Button won't overlap and hide the Column's text (this procedure needs to be fine-tuned, in relation to the Button's specifics).
To replace the current HeaderCell
of a Column (e.g., Column[0]
), just create a new instance of the custom HeaderCell
and call the ReplaceHeaderCell()
method, passing the reference of the HeaderCell
to replace:
var newButtonHeaderCell = new DGVButtonHeaderCell();
newButtonHeaderCell.ReplaceHeaderCell(dataGridView1.Columns[0].HeaderCell);
This is how it works:
using System.Drawing;
using System.Windows.Forms;
class DGVButtonHeaderCell : DataGridViewColumnHeaderCell
{
public readonly Button button;
private Padding m_ButtonPadding = Padding.Empty;
public DGVButtonHeaderCell(int buttonWidth = 40)
{
button = new Button() { BackColor = Color.Orange, Width = buttonWidth };
m_ButtonPadding = new Padding(button.Width + 2, 0, 0, 0);
button.Click += ButtonClick;
}
public void ReplaceHeaderCell(DataGridViewColumnHeaderCell previousHeader)
{
if (previousHeader == null) return;
SetStandardValues(previousHeader);
var dgv = previousHeader.DataGridView;
dgv.Columns[previousHeader.OwningColumn.Index].HeaderCell = this;
previousHeader.Dispose();
}
private void SetStandardValues(DataGridViewColumnHeaderCell previous)
{
if (previous == null) return;
this.Style = previous.Style;
button.Font = this.Style.Font ?? previous.DataGridView.ColumnHeadersDefaultCellStyle.Font;
// etc.
}
private void ButtonClick(object sender, EventArgs e)
{
OnClick(new DataGridViewCellEventArgs(ColumnIndex, RowIndex));
MessageBox.Show("You clicked me");
}
protected override void OnDataGridViewChanged()
{
if (this.DataGridView == null) return;
this.DataGridView.Controls.Add(button);
}
protected override void Paint(Graphics g, Rectangle clipBounds, Rectangle bounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advBorderStyle, DataGridViewPaintParts parts)
{
cellStyle.Padding = Padding.Add(m_ButtonPadding, cellStyle.Padding);
base.Paint(g, clipBounds, bounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advBorderStyle, parts);
button.Location = new Point(bounds.Left, bounds.Top + 2);
button.Height = bounds.Height - 4;
}
protected override void Dispose(bool disposing)
{
if (disposing) {
button.MouseClick -= ButtonClick;
button.Dispose();
}
base.Dispose(disposing);
}
}
这篇关于如何用自定义标题替换 DataGridView 的 HeaderCells?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!