Format expression Applied to this data type Description Price: {0:C} Note The {0} is a zero, not the letter O. numeric/decimal Displays the literal "Price:" followed by numbers in currency format. The currency format depends on the culture setting specified via the culture attribute on the Page directive or in the Web.config file. {0:D4} integer (Cannot be used with decimal numbers.) Integers are displayed in a zero-padded field four characters wide. {0:N2}% numeric Displays the number with 2-decimal place precision followed by the literal "%". {0:000.0} numeric/decimal Numbers rounded to one decimal place. Numbers less than three digits are zero padded. {0:D} date/datetime Long date format ("Thursday, August 06, 1996"). Date format depends on the culture settting of the page or the Web.config file. {0:d} date/datetime Short date format ("12/31/99"). {0:yy-MM-dd} date/datetime Date in numeric year-month-day format (96-08-06).
Showing and Hiding Columns Dynamically One way to have columns appear dynamically is to create them at design time, and then to hide or show them as needed. You can do this by setting a column's Visible property. The following example shows how to toggle the visibility of the second column (index 1) of the grid:
' Visual Basic DataGrid1.Columns(1).Visible = Not (DataGrid1.Columns(1).Visible)
// C# DataGrid1.Columns[1].Visible = !(DataGrid1.Columns[1].Visible); Adding Columns Dynamically You can hide and show columns if you know in advance what columns you need. Sometimes, however, you do not know that until run time. In that case, you can create columns dynamically and add them to the grid.
To do so, you create an instance of one of the column classes supported by the grid — BoundColumn, EditCommandColumn, ButtonColumn, or HyperlinkColumn. (You can add template columns to the grid, but it is slightly more complex. For details, see Creating Web Server Control Templates Programmatically.) Set the column's properties, and then add it to the grid's Columns collection.
The following example shows how to add two bound columns to a grid.
' Visual Basic Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click 'Set data-binding properties of the grid DataGrid1.AutoGenerateColumns = False DataGrid1.DataSource = Me.dsBooks1 DataGrid1.DataMember = "Books" DataGrid1.DataKeyField = "bookid"