Multiple Column Dropdownlist for the ASP.NET DataGrid

转载 2006年06月23日 16:00:00

Image showing when a control is added to EditItemTemplate.


Based on my previous control "Multiple Column DropDownList for ASP.NET", I received many emails asking for the same control to be used in the DataGrid for web applications. Here we go.. This control can be used as the regular MS DropDownList in the DataGrid and also as a regular dropdownlist. It has all the properties, like DataTextField, DataValueField, DataSource, SelectedIndex etc. The download file contains the samples both in VB.NET and C#. In this sample, I have used the Northwind database of SQL Server.

Building the Control

The following web server controls were used to build this control:

  1. TextBox
  2. Label
  3. Panel

Adding the Control to DataGrid

This control can be added either to the ItemTemplate or EditItemTemplate of the DataGrid through Property Builder.

  1. Drop a DataGrid onto your webform.
  2. Right click the DataGrid and select Property Builder and select Columns.
  3. A bound column may or may not be required, I have shown samples of both.
  4. Add two bound columns, set the header and the text field: SupplierID and CompanyName.
  5. Add one Template Column and set the header text to Products.
  6. Uncheck the checkbox "Create columns automatically at run time".
  7. Click OK.
  8. Now right click the DataGrid and select Edit Template, now either you could add this in ItemTemplate or EditItemTemplate.
  9. Set the properties of the control, like CssClass, GridRowcolor, DataTextfield etc.
  10. Close the template box.

Points to Note:

The DataValueField property is readonly, which means you cannot set the value, so make sure that in your query, the row value field is always the first column, which will not be displayed in the dropdown. The DataTextField returns an integer value, so while setting this property, type the column position from your "SELECT" statement, which needs to get displayed in the textbox. (For e.g., to display the second column "LastName" in the textbox, set DataValueField =2.) If your DataGrid is inside the "Table/Div/Span", then make sure the "Position" attribute in the "Style" property is set to absolute, and if not used in any of these tags, then set the DataGrid's Style property.

Make sure that the HorizontalAlign and VerticalAlign properties of the ItemStyle of your DataGrid is set as shown, else the control may not be positioned properly inside the DataGrid. E.g.:

<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>

See the code below. E.g., to populate the dropdown with FirstName and LastName:

SELECT Employeeid,Firstname,LastName
From Employees

Now if added in the EditItemTemplate (based on your requirement, bounded column may or may not be required), in my sample I used two <BoundColumn>s:

<ASP:DATAGRID id="jskDataGrid" runat="server" 
    Font-Size="8pt" HeaderStyle-ForeColor="Tan" 
    AutoGenerateColumns="False" HeaderStyle-BackColor="Maroon" 
    HeaderStyle-Font-Bold="True" Font-Name="Verdana" ShowFooter="false" 
    BorderColor="Tan" DataKeyField="SupplierID" 
    OnUpdateCommand="MyDataGrid_Update" OnCancelCommand="MyDataGrid_Cancel" 
    OnEditCommand="MyDataGrid_Edit" CellSpacing="0" CellPadding="3" 
    Width="800" Style="Position:absolute;Top:0px;Left:0px">

  <HeaderStyle Font-Bold="True" ForeColor="Tan" BackColor="Maroon"></HeaderStyle>
        <asp:EditCommandColumn ButtonType="LinkButton" 
             UpdateText="Update" CancelText="Cancel" EditText="Edit">
          <ItemStyle VerticalAlign="Top"></ItemStyle>
        <asp:BoundColumn DataField="SupplierID" 
              ReadOnly="True" HeaderText="Supplier ID">
          <ItemStyle Wrap="False" VerticalAlign="Top"></ItemStyle>
        <asp:BoundColumn DataField="CompanyName" 
              ReadOnly="True" HeaderText="Company Name">
          <ItemStyle Wrap="False" VerticalAlign="Top"></ItemStyle>
        <asp:TemplateColumn HeaderText="Products">
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
        <!-- Set these two properties as shown-->
        <%# DataBinder.Eval(Container.DataItem, "ProductName") %>
            <TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
                  <jsk:mcDDList id="McDDList1" 
                    SelectedIndex='<%# DataBinder.Eval(Container.DataItem, 
                                                          "ProductID") %>' 
                    cssClass="cStyle" Runat="server" 
                    DataSource="<%# PopulateDDList()%>"

If you want to show the existing values then add the "SelectedIndex" property.


  • DataTextField - The field to be shown in the Ccontrol.
  • DataValueField - Read only property (value field to identify the row - default is first column {0}).
  • DataSource - DataSet to populate the dropdown.
  • DDTextboxReadonly - If false, can type your own text.
  • GridRowColor - OnMouseMove changes the row color.
  • GridTextColor - OnMouseMove changes the text color.
  • ListBoxHeight - Sets the height of the dropdown.
  • ReturnsValue - To get the text, set it to false. To get the value, set it to true, while updating.
  • SelectedIndex - If set, shows the existing value in the dropdown.

Populating the DataGrid

private void Page_Load(object sender, System.EventArgs e)
    // Put user code to initialize the page here
    if (! IsPostBack)

protected void BindDataGrid()
    string sqlStr = "SELECT S.CompanyName, S.SupplierID," +
                    " P.ProductName, P.ProductID " +
                    "from Suppliers S inner join Products P " +
                    "on S.SupplierID = P.SupplierID ";
    sqlDa = new SqlDataAdapter(sqlStr, SqlCon);
    ds = new DataSet();
    sqlDa.Fill(ds, "Prod");
    jskDataGrid.DataSource = ds.Tables["Prod"];

Populating the DropDownList

public DataSet PopulateDDList()
    string sqlString = " select ProductID, ProductName, " +
                       "CategoryName as Name,UnitPrice as Price " +
                       "from Products p inner join Categories c " +
                       "on p.categoryid = c.categoryid ";
    SqlDataAdapter ad = new SqlDataAdapter(sqlString,SqlCon);
    DataSet ds = new DataSet();
    return ds;

Codes to Edit/Update/Cancel

protected void jskDataGrid_Edit(object sender, DataGridCommandEventArgs e)
    jskDataGrid.EditItemIndex = e.Item.ItemIndex;

protected void jskDataGrid_Cancel(object sender, DataGridCommandEventArgs e)
    jskDataGrid.EditItemIndex = -1;

protected void jskDataGrid_Update(object sender, DataGridCommandEventArgs e)
        string itemValue;
        string itemText;

        // To get the DataValueField of the DropDown
        ((ddList.mcDDList)e.Item.FindControl("McDDList1")).ReturnsValue = true;
        itemValue =

        // To get the DataTextField of the Dropdown
        ((ddList.mcDDList)e.Item.FindControl("McDDList1")).ReturnsValue = false;
        itemText =

        //write your update query
        //update table set col1 = itemtext where col2 = itemvalue
        //Execute the Query

        jskDataGrid.EditItemIndex = -1;
    catch(Exception ex)
        /*Close your Connection */

Points of Interest

The control is built mostly using JavaScript. There is a property "DDTextboxReadonly", this property is used to enable/disable the textbox, means either you could select the text from the list or you could type.

Using the Intellisense

As in my previous article, I showed how to use the Intellisense. The zip file contains the .xsd, copy it to the following location:

  • C:/Program Files/Microsoft Visual Studio .NET 2003/Common7/Packages/schemas/xml

and in the <body> tag of your aspx page, add the following code:

<body MS_POSITIONING="GridLayout"


  • ver. (1.0) - Initial version (posted 12th May 2005)
  • ver. (1.1) - New property has been added, ListBoxHeight to adjust the height of the dropdown.
  • ver. (1.2) - Control does not expand the DataGrid rows.

ASP.NET MVC DropdownList 使用

ASP.NET MVC 使用Html Helper的形式实现UI控件,里面有一个非常重要的下拉列表控件-DropdownList。这个控件使用还是比较灵活的,今天总结了一下。 1.View @us...
  • afandaafandaafanda
  • afandaafandaafanda
  • 2015年06月08日 16:32
  • 748


 前台代码:  asp:DataGrid DataKeyField="id" id="DataGrid1" runat="server"  AutoGenerateColumns="False">  ...
  • chenyuling
  • chenyuling
  • 2007年04月10日 16:00
  • 993


@ Page language="c#" Codebehind="DataGrid加单选框.aspx.cs" AutoEventWireup="false" Inherits="bsTest2005_...
  • szp1118
  • szp1118
  • 2006年11月15日 14:12
  • 680


  • bill1315
  • bill1315
  • 2007年10月09日 15:31
  • 510


第一种少量自定义数据时: .aspx中的代码: 男 女 .cs中的代码: 注意后...
  • q383965374
  • q383965374
  • 2013年05月12日 16:16
  • 11333


.aspx代码如下: AutoPostBack="True" > ...
  • yayun0516
  • yayun0516
  • 2014年12月08日 10:02
  • 1844 DropDownList数据绑定及使用详解

1.       dropdownlist 绑定数据 1.1  固定绑定(适合已经固定的数据绑定到dropdownlist) 实例: 选择性别 男 女     1.2  动态...
  • admindong
  • admindong
  • 2016年09月13日 20:18
  • 248

关于 ASP.NET 中DropDownList 事件的用法

我刚刚开始学习ASP.NET,所以当然有点辛苦,懂的不是很多...今天写程序时碰到了控件 DrowDownList.--列表控件....当我改变控件的值时,怎么也不能改变另一个DropDownList...
  • wangzi041
  • wangzi041
  • 2007年11月17日 13:04
  • 6077

column name was specified multiple times

调试SQL时出现如下提示: Msg 8156, Level 16, State 1, Line 2 The column 'customer' was specified multiple...
  • qingyushan
  • qingyushan
  • 2016年08月18日 21:56
  • 434 可选可输入的dropdownlist

  • yiwangxiblog
  • yiwangxiblog
  • 2016年04月23日 15:40
  • 2386
您举报文章:Multiple Column Dropdownlist for the ASP.NET DataGrid