Introduction:
There are situations when you need the columns to be created dynamically. Creating dynamic columns in GridView control is almost identical to Datagrid control. If you are working with Datagrid control than check out my article Creating Dynamic Bound and Template Columns in Datagrid Control. In this article we will see how to create dynamic bound and template columns for the GridView.
Dynamically Creating BoundColumn for GridView:
BoundColumns are pretty straight forward. Check out the code below:
BoundField nameColumn = new BoundField();
nameColumn.DataField = "Name";
nameColumn.HeaderText = "Person Name";
GridView1.Columns.Add(nameColumn);
|
All you do is make an object of the BoundField class. Assign the DataField, HeaderText and finally add it to the GridView columns collection. There must be any source from which we get the data field and in this case the source is SqlDataSource object. For more information about how to assign SqlDataSource to the GridView control please check out my GridView articles.
Dynamically Creating a Template Column:
Now let's see how we can create template columns dynamically. Since template columns can contains any control you must define a custom template which creates the controls that are identified. You can download the template file which is present in the zip file at the end of this article.
The code below shows how you can use the custom template "GridViewTemplate.cs".
TemplateField ckhColumn = new TemplateField();
ckhColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "CheckBox Column");
ckhColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");
GridView1.Columns.Add(ckhColumn);
|
First we make an instance of the TemplateField class. Than we set the HeaderTemplate type by passing the ListItemType.Header and the name of the template column to be created. Than we set the ItemTemplate and finally we add the newly created template column in the GridView columns collection.
Since I am using SqlDataSource object it will take care of binding for me but if you are using a different data source than you must call GridView1.DataBind() method to bind it on the screen.
Please view GridViewTemplate.cs file which contains the main code to create the template columns.
I hope you liked the article, happy coding!
|
By AzamSharp
|
|
http://gridviewguy.com/ArticleDetails.aspx?articleID=88
分享到:
相关推荐
Dynamically sizing grid rows/columns 90 Creating a scrollable user interface 92 Creating a border around panels and elements 94 Placing elements in exact positions 96 Adding/removing elements to ...
GRASP实验室飞控技术:We study the problem of designing dynamically feasible trajectories and controllers that drive a quadrotor to a desired state in state space. We focus on the development of a ...
Dynamically Creating, Building and Mapping a Context in Web Dynpro
This book is for developers with a basic understanding of Android programming who would like to improve the appearance and usability of their applications by creating a more interactive user ...
Creating Excel Charts Dynamically from Database, using VB.NET
A reconfigurable antenna is an antenna capable of modifying dynamically its frequency and radiation properties in a controlled and reversible manner.[2] In order to provide a dynamical response, ...
(and new behavior) by plugging in a customized template. You’ll also see how templates allow you to build a skinnable application. Chapter 18: Custom Elements explores how you can extend the ...
JavaScript
Identify the high level steps such as verifying user input, using command lines and conditional statements in creating and executing simple shell scripts Create and edit dynamic shell scripts to ...
You’ll walk through creating microservices in Go, adding front-end web components using ReactJS and Flux, and mastering advanced Go-based cloud-native techniques. Hoffman and Nemeth show how to build...
With Spring DM, you can easily create highly modular applications and you can dynamically add, remove, and update your modules. Spring Dynamic Modules in Action is a comprehensive tutorial that ...
Response context and template information 131 Testing transactional behavior 132 Chapter 5: Filling in the Blanks: Integrating Django and Other Test Tools 135 Problems of integration 136 ...
which enables a quadrotor to use its dynamics in order to learn about the payload it is carrying and also adapt its control law in order to improve tracking performance. In Ch. 5 we present a ...
adaptive mesh refinement fortran\An Efficient Dynamically Adaptive Mesh for potentially singular solutions
47. What are copulas and how are they used in quantitative finance? 212 48. What is the asymptotic analysis and how is it used in financial modelling? 216 49. What is a free-boundary problem and what ...
where sensor nodes join the Internet dynamically, and use it to collaborate and accomplish their tasks. However, when WSNs become a part of the Internet, we must carefully investigate and analyze the ...
Order and arrange nodes in an efficient, well-reasoned and carefully structured way Create a fully-functional TileMap with auto-tiling to easily create new levels for your video games Design elegant ...
DB - Unbounded Pipelining in Dynamically Reconfigurable Paxos Clusters.pdf Consensus is an essential ingredient of a faulttolerant distributed system systems. When equipped with a consensus ...
Networking in the Background using AsyncTask and AsyncTaskLoader Summary Chapter 16 Interaction and Animation: Live Wallpaper and Handlers Live Wallpaper Handler Summary Index Colophon