加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在datagridview中为单元格创建页脚

发布时间:2020-12-15 23:24:47 所属栏目:百科 来源:网络整理
导读:我需要使用包含两个部分的单元格创建DataGridView.一部分是该单元格的内容,例如0,1等值.剩下的部分是该单元格的页脚,就像单词文档的页脚一样,指的是该单元格的序数. 我无法附上任何图像,因此问题可能不明确. 无论如何,提前谢谢. 解决方法 要使用额外内容创建
我需要使用包含两个部分的单元格创建DataGridView.一部分是该单元格的内容,例如0,1等值.剩下的部分是该单元格的页脚,就像单词文档的页脚一样,指的是该单元格的序数.

我无法附上任何图像,因此问题可能不明确.

无论如何,提前谢谢.

解决方法

要使用额外内容创建DataGridView单元,您需要对CellPainting事件进行编码.

首先,您将单元格设置为有足够的空间容纳额外内容,并根据需要布置正常内容..:

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中.

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;
    }
}

对于较长的多行脚注,您可以使用边界矩形而不仅仅是x& y坐标.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读