How to Add Columns in Datagrid

How to Add Columns in Datagrid thumbnail
A DataGrid lays out data in rows and columns.

The "DataGrid" object in ASP.NET provides developers with the ability to list data in a table-like structure. The DataGrid is similar to a table, but it has several functions and properties that give developers the tools to format the data. One option for programmers is the function to add columns to the DataGrid in the code. You can add one or several columns at one time to your web page's DataGrid object.

Instructions

    • 1

      Define a new data column. This column is later added to the grid, but you need a data column object defined to set the format settings for the column. The following code creates a data column:

      Dim column As DataColumn

    • 2

      Assign the data column's text header and data type. The column text defines the header, which is shown at the top of the column. The data type defines what type of data you can insert into the DataGrid's column. In this example, the "Customer Name" column is created and it contains a string. The following code creates the column's settings:

      dc = New DataColumn("Customer Name", Type.GetType("System.String"))

    • 3

      Add the column to the DataGrid. The DataGrid has an "Add()" function that allows you to insert a new column dynamically. The following code adds the column created in step two to your DataGrid:

      datagrid1.Columns.Add(column)

    • 4

      Save your code changes and open the page in your browser. The column is created in the grid with the "Customer Name" header text.

Related Searches

References

  • Photo Credit red and green discs in a red grid image by nebari from Fotolia.com

You May Also Like

Related Ads

Featured
View Mobile Site