Friday, July 3, 2009

Creating an ASP.NET GridView Custom Field of type DropDownList

Introduction

Sometimes, you need to use a DropDownList in a GridView in a way to see the text of fields and save values in to your database. Here, we have extended the “BoundField” class and added features to create a column type of “DropDownList”.

For example, in this picture, you can see two tables, Customer and Category.

Suppose you need to edit the content of the Customer table with a GridView and select “Customer Category” through a “DropDownList”. You want to see the CategoryName in the grid but save CategoryID in to the database, like in this picture:

GridEdit.jpg

Background

You have two solutions:

  • The first solution is to create a Template column of type DropDownList.
  • The second solution is to create a new column type with the “DropDownList” editor and introduce an “Entity Name” for it. Here, we have extended the “BoundField” class and added features to create a column type of “DropDownList”.

Using the code

Now, you can use this column type easily, like:


See full detail: http://www.codeproject.com/KB/webforms/GridViewComboBoxField.aspx

No comments: