簡體   English   中英

如何在datagridview中為單元格創建頁腳

[英]How can I create a footer for cell in datagridview

我需要使用包含兩個部分的單元格創建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 對於包含更多字體等的更復雜的東西,你需要創建一個類或結構來保存它,也許還可以在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坐標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM