如何在 datagridview 中为单元格创建页脚
How can I create a footer for cell in datagridview
我需要创建一个包含两部分单元格的 DataGridView。一部分是该单元格的内容,例如 0、1 等值。而剩下的部分就是该单元格的页脚,就像word文档的页脚一样,指的是该单元格的序号。
我无法附上任何图片,所以问题可能不明确。
无论如何,先谢谢了。
要创建包含额外内容的 DataGridView
单元格,您需要对 CellPainting
事件进行编码。
首先,您设置单元格以便为额外内容留出足够的空间,并根据需要布置正常内容..:[=20=]
DataGridView DGV = dataGridView1; // quick reference
Font fatFont = new Font("Arial Black", 22f);
DGV .DefaultCellStyle.Font = fatFont;
DGV .RowTemplate.Height = 70;
DGV .DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
接下来我填写一些内容;我将额外的内容添加到单元格的 Tags
。对于具有更多字体等的更复杂的东西,您将需要创建一个 class 或结构来保存它,也许也在 Tags
..
DGV.Rows.Clear();
DGV.Rows.Add(3);
DGV[1, 0].Value = "Na"; DGV[1, 0].Tag = "Natrium";
DGV[1, 1].Value = "Fe"; DGV[1, 1].Tag = "Ferrum";
DGV[1, 2].Value = "Au"; DGV[1, 2].Tag = "Aurum";
下面是 CellPainting
事件的编码示例:
private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex < 0) return; // header? nothing to do!
if (e.ColumnIndex == yourAnnotatedColumnIndex )
{
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
string footnote = "";
if (cell.Tag != null) footnote = cell.Tag.ToString();
int y = e.CellBounds.Bottom - 15; // pick your font height
e.PaintBackground(e.ClipBounds, true); // show selection? why not..
e.PaintContent(e.ClipBounds); // normal content
using (Font smallFont = new Font("Times", 8f))
e.Graphics.DrawString(footnote, smallFont,
cell.Selected ? Brushes.White : Brushes.Black, e.CellBounds.Left, y);
e.Handled = true;
}
}
对于较长的多行脚注,您可以使用边界 Rectangle
而不仅仅是 x&y 坐标..
我需要创建一个包含两部分单元格的 DataGridView。一部分是该单元格的内容,例如 0、1 等值。而剩下的部分就是该单元格的页脚,就像word文档的页脚一样,指的是该单元格的序号。
我无法附上任何图片,所以问题可能不明确。
无论如何,先谢谢了。
要创建包含额外内容的 DataGridView
单元格,您需要对 CellPainting
事件进行编码。
首先,您设置单元格以便为额外内容留出足够的空间,并根据需要布置正常内容..:[=20=]
DataGridView DGV = dataGridView1; // quick reference
Font fatFont = new Font("Arial Black", 22f);
DGV .DefaultCellStyle.Font = fatFont;
DGV .RowTemplate.Height = 70;
DGV .DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
接下来我填写一些内容;我将额外的内容添加到单元格的 Tags
。对于具有更多字体等的更复杂的东西,您将需要创建一个 class 或结构来保存它,也许也在 Tags
..
DGV.Rows.Clear();
DGV.Rows.Add(3);
DGV[1, 0].Value = "Na"; DGV[1, 0].Tag = "Natrium";
DGV[1, 1].Value = "Fe"; DGV[1, 1].Tag = "Ferrum";
DGV[1, 2].Value = "Au"; DGV[1, 2].Tag = "Aurum";
下面是 CellPainting
事件的编码示例:
private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex < 0) return; // header? nothing to do!
if (e.ColumnIndex == yourAnnotatedColumnIndex )
{
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
string footnote = "";
if (cell.Tag != null) footnote = cell.Tag.ToString();
int y = e.CellBounds.Bottom - 15; // pick your font height
e.PaintBackground(e.ClipBounds, true); // show selection? why not..
e.PaintContent(e.ClipBounds); // normal content
using (Font smallFont = new Font("Times", 8f))
e.Graphics.DrawString(footnote, smallFont,
cell.Selected ? Brushes.White : Brushes.Black, e.CellBounds.Left, y);
e.Handled = true;
}
}
对于较长的多行脚注,您可以使用边界 Rectangle
而不仅仅是 x&y 坐标..