How can I create a footer for cell in datagridview

Thư Trần

I need to create a DataGridView with cells that have two parts. One part is the content of that cell such as 0, 1 etc value. And the remain part is the footer of that cell, just like a footer of a word document, refers to the ordinal number of that cell.

I can not enclose any images so the question may be ambiguous.

Anyways thanks in advance.

TaW

enter image description here

To create DataGridView cells with extra content you need to code the CellPainting event.

First you set up the cells to have enough room for the extra content and layout the normal content as you wish..:

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;

Next I fill in some content; I add the extra content to the cells' Tags. For more complicated things with more fonts etc, you will want to create a class or stucture to hold it, maybe also in the 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";

Here is an example of coding the CellPainting event:

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

For longer multiline footnotes you can use a bounding Rectangle instead of just the x&y coordinates..

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I create a footer for cell in datagridview

From Dev

How can I compare datagridview cell of datagridview1 with another datagridview cell of datagridview2 in C#

From Dev

How can I retrieve the previous value of a DataGridView cell using the CellValueChanged event?

From Dev

How can I extract semicolon separated values from DataGridView cell in C#

From Dev

How can I detect the cell value changed of a specific datagridview column? - VB.NET

From Dev

How can i change color of particular row in datagridview, where a cell value is first timestamp of each day?

From Dev

How can I pass multiple cell values of datagridview from one form to another at same time in C#?

From Dev

For a Windows Forms Application using `DataGridView`, how can I check a value in my DataSource and change the color of a separate cell?

From Dev

How can I Create a Tableview Under Custom Tableview cell

From Dev

Can I change my datagridview cell font style based on a condition?

From Dev

CSS - How I can cut the height footer

From Dev

How can I provide a header or a footer for an MvxCollectionViewController?

From Dev

How can i set the footer to the bottom of the page?

From Dev

How can I change footer background color?

From Dev

How can i set the footer to the bottom of the page?

From Dev

How to delete a datagridview cell?

From Dev

How can I create a DataGridView in a TabPage of 2nd from by clicking the button in the 1st Form

From Dev

How can store a datagridview as a array in a cell of a mysql table

From Dev

How do I track the old value in a datagridview cell?

From Dev

How do I track the old value in a datagridview cell?

From Dev

How to get the data when I select a datagridview cell or column

From Dev

C#: How do I get the value of an unbound DataGridView cell?

From Dev

How I can save a DatagridView in a Xml and Load A Xml to datagridView?

From Dev

How can I read the next DataGridView row?

From Dev

How can I manipulate data that is in a DataGridView box?

From Dev

How to remove the last/footer-row in DataGridView?

From Dev

How can I create a cell array in matlab combining text and number matrices?

From Dev

How do I create a combobox in active cell?

From Dev

How to add a Cell programmatically to a DataGridView?

Related Related

  1. 1

    How can I create a footer for cell in datagridview

  2. 2

    How can I compare datagridview cell of datagridview1 with another datagridview cell of datagridview2 in C#

  3. 3

    How can I retrieve the previous value of a DataGridView cell using the CellValueChanged event?

  4. 4

    How can I extract semicolon separated values from DataGridView cell in C#

  5. 5

    How can I detect the cell value changed of a specific datagridview column? - VB.NET

  6. 6

    How can i change color of particular row in datagridview, where a cell value is first timestamp of each day?

  7. 7

    How can I pass multiple cell values of datagridview from one form to another at same time in C#?

  8. 8

    For a Windows Forms Application using `DataGridView`, how can I check a value in my DataSource and change the color of a separate cell?

  9. 9

    How can I Create a Tableview Under Custom Tableview cell

  10. 10

    Can I change my datagridview cell font style based on a condition?

  11. 11

    CSS - How I can cut the height footer

  12. 12

    How can I provide a header or a footer for an MvxCollectionViewController?

  13. 13

    How can i set the footer to the bottom of the page?

  14. 14

    How can I change footer background color?

  15. 15

    How can i set the footer to the bottom of the page?

  16. 16

    How to delete a datagridview cell?

  17. 17

    How can I create a DataGridView in a TabPage of 2nd from by clicking the button in the 1st Form

  18. 18

    How can store a datagridview as a array in a cell of a mysql table

  19. 19

    How do I track the old value in a datagridview cell?

  20. 20

    How do I track the old value in a datagridview cell?

  21. 21

    How to get the data when I select a datagridview cell or column

  22. 22

    C#: How do I get the value of an unbound DataGridView cell?

  23. 23

    How I can save a DatagridView in a Xml and Load A Xml to datagridView?

  24. 24

    How can I read the next DataGridView row?

  25. 25

    How can I manipulate data that is in a DataGridView box?

  26. 26

    How to remove the last/footer-row in DataGridView?

  27. 27

    How can I create a cell array in matlab combining text and number matrices?

  28. 28

    How do I create a combobox in active cell?

  29. 29

    How to add a Cell programmatically to a DataGridView?

HotTag

Archive