Format DataGridView columns in C#

[Format DataGridView columns]

This example shows how you can format DataGridView columns to make them display values properly. The control is actually pretty smart and will usually do the right thing if you give it the right data. For example, if a column contains numeric data, it will sort that column numerically rather than textually. For example, 2 comes before 12 numerically but 12 comes before 2 when treated as text.

The most important step is feeding the control the right data types. If a column should hold a numeric value, pass the control numeric data. For example, the following statement adds a new row to this example’s DataGridView control.

dgvValues.Rows.Add("Notebook", 1.17m, 6m, 1.17m * 6m);

The second, third, and fourth data values are numeric so the control automatically sorts them numerically.


[Format DataGridView columns]
You can also customize the way the control displays its data. To do that, select the control in the Form Designer, click the smart tag (the little triangle) in its upper right corner, and select Edit Columns. IN the Edit Columns dialog shown on the right, you can set the column’s name, header text, width, column type (which can be button, checkbox, combobox, image, link, or textbox), and other properties.


[Format DataGridView columns]
To set the column’s formatting, click the DefaultCellType entry and click the ellipsis on the right to see the dialog shown on the right. Here you can set several useful properties. For example, you can:

  • Use the Alignment property to right-align numeric values
  • Set Format to C2 for currency with two digits after the decimal place or N0 for numbers with no digits after the decimal place
  • Set NullValue to the value you want the control to display if a data value in the column is null

In summary, the DataGridView control is quite powerful, if you give it the right kinds of data and a few hints about column formatting.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in controls and tagged , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Format DataGridView columns in C#

  1. mgmg says:

    I want to know SQL Data type money. I want to show two decimal at My form DataGridView how to fit it I fit C2 , N2 all is not working ..

    • RodStephens says:

      The only reason I can think of that would make C2 or N2 not work is if the data is not being selected as a numeric type. For example, if the code is selecting it as a string, then C2 or N2 wouldn’t work on it.

      Otherwise you could add the values to the control as currency values. Instead of this:

      dgvValues.Rows.Add("Notebook", 1.17m, 6m, 1.17m * 6m);

      You could do this:

      dgvValues.Rows.Add("Notebook", "$1.17", "6", "$7.02");

      If you’re loading values from a database, you’ll have to write the code to convert the values into strings and then add those.

Leave a Reply

Your email address will not be published. Required fields are marked *