C# 如何在datagridview中为单元格创建页脚
我需要用包含两部分的单元格创建DataGridView。一部分是该单元格的内容,例如0、1等值。剩下的部分是该单元格的页脚,就像word文档的页脚一样,是指该单元格的序号 我无法附上任何图片,因此问题可能不明确 无论如何,先谢谢你。 要创建包含额外内容的C# 如何在datagridview中为单元格创建页脚,c#,winforms,datagridview,cell,C#,Winforms,Datagridview,Cell,我需要用包含两部分的单元格创建DataGridView。一部分是该单元格的内容,例如0、1等值。剩下的部分是该单元格的页脚,就像word文档的页脚一样,是指该单元格的序号 我无法附上任何图片,因此问题可能不明确 无论如何,先谢谢你。 要创建包含额外内容的DataGridView单元格,需要对CellPaint事件进行编码 首先,设置单元格,使其有足够的空间容纳额外的内容,并根据需要布局正常的内容..: DataGridView DGV = dataGridView1; // quick ref
DataGridView
单元格,需要对CellPaint
事件进行编码
首先,设置单元格,使其有足够的空间容纳额外的内容,并根据需要布局正常的内容..:
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;
接下来我填写一些内容;我将额外的内容添加到单元格的标记中
。对于具有更多字体等的更复杂的内容,您可能需要创建一个类或结构来保存它,也可能在标记中
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;
}
}
private void dataGridView1\u CellPaint(对象发送器,
DataGridViewCellPaintingEventArgs(e)
{
如果(e.RowIndex<0)返回;//头?无需执行任何操作!
如果(e.ColumnIndex==yourAnnotatedColumnIndex)
{
DataGridViewCell单元格=dataGridView1[e.ColumnIndex,e.RowIndex];
字符串脚注=”;
如果(cell.Tag!=null)footnote=cell.Tag.ToString();
int y=e.CellBounds.Bottom-15;//选择字体高度
e、 PaintBackground(例如剪贴簿,true);//显示所选内容?为什么不。。
e、 PaintContent(例如剪贴簿);//正常内容
使用(字体smallFont=新字体(“Times”,8f))
e、 图形.抽绳(脚注,小字体,
单元格。选定?画笔。白色:画笔。黑色,e.CellBounds。左侧,y);
e、 已处理=正确;
}
}
对于较长的多行脚注,您可以使用边界矩形
,而不仅仅是x&y坐标。请向我们展示您迄今为止的尝试。请改用listview。否则,您必须自定义datagridview并添加页脚功能,您可以从listview复制该功能。您看过我的答案广告了吗?它看起来像您想要做的吗?对一个模棱两可的问题的可靠答案。干得好谢谢你,我知道了。抱歉没有回复,我最近很忙。是DataGridView DGV=DataGridView 2代码>应该是2还是1?谢谢。这个答案太棒了。还有,e.PaintContent(e.ClipBounds)的意义是什么代码>?当我删除它时,我会得到相同的结果。可能是因为您没有任何“正常”内容吗?