Click here to Skip to main content
15,884,298 members
Articles / Web Development / ASP.NET
Article

Multiple selection using DropDownList in DataGrid

Rate me:
Please Sign up or sign in to vote.
3.54/5 (25 votes)
13 Aug 20043 min read 209.6K   49   12
Multiple selection using DropDownList in DataGrid.

Introduction:

In this article, I will explain how to select multiple DropDownList values when the DropDownList is one of the columns of the DataGrid. I am using Northwind database in this example which is the default database in SQL Server.

Adding DropDownList to DataGrid:

Okay, adding DropDownList to DataGrid is straightforward. Read the following steps:

  1. Drag and drop DataGrid onto the webform.
  2. Right click on DataGrid -> select Property Builder.
  3. In Property Builder, select Columns.
  4. Add two bound columns and one template column. (Remember to add bound columns first and than the template column.) Uncheck Create columns automatically.
  5. Now, in the same window, click on the bound column appearing on the right window and set its header text to "Category ID" and DataField to "CategoryID". Click on the second bound column appearing in the right window and set header text to "Category Name" and DataField to "CategoryName".

Remember that header text is just the pure plain text which can be anything. DataField on the other hand is the name of the column in the database, so it must be the exact same name as of the column in database.

Now, it's time to add the DropDownList. Simple right click on DataGrid and select the option Edit template. In the item template, just drag and drop a DropDownList in item template.

Also, add one HTML Button control anywhere on the form.

Once you have done all these steps, your DataGrid should look something like this:

Sample screenshot

Populating DataGrid with Data:

Populating DataGrid is straight forward, just make the data source run a query and bind the source to the DataGrid.

C#
private void Page_Load(object sender, System.EventArgs e)
{
  if(!Page.IsPostBack)
  {
    BindData();
    PopulateList();
  }
}

public void BindData() 
{ 
  SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories",myConnection);
  DataSet ds = new DataSet();
  ad.Fill(ds,"Categories");
  myDataGrid.DataSource = ds;
  myDataGrid.DataBind();
}

In the code above, I have used Ad-Hoc query, a query in string format. You should never execute your query like this, and always use stored procedures for security purposes.

Populating DropDownList from database:

You can always populate your DropDownList by using its Collections property, but to make things interesting, I am going to populate the DropDownList with the data coming from the database column.

C#
public DataSet PopulateList()
{
  SqlDataAdapter ad = new 
    SqlDataAdapter("SELECT Description FROM Categories", myConnection);

  DataSet ds = new DataSet();
  ad.Fill(ds,"Categories");
  return ds;
}

As you see in the above code, the method PopulateList is returning a DataSet. This DataSet is used by the DropDownList to populate itself. Let's see how DropDownList named "ddlList" uses this DataSet to populate itself.

In the aspx page HTML, find the line where DropDownList is declared, and add properties to it.

ASP.NET
<asp:DropDownList id="ddlList" runat="server" 
    DataSource="<%# PopulateList %>" DataTextField="Description" 
    DataValueField="Description" >

After this, your DropDownList will be populated with the data coming from "Description" column of the NorthWind database.

Here is a picture of the complete WebForm:

Sample screenshot

Button Click Event Code:

Remember that we added a HTML Button control. In the Click event of the Button, write this code:

C#
private void Button1_Click(object sender, System.EventArgs e)
{
  string name = null;
  foreach(DataGridItem dgi in myDataGrid.Items)
  {
    DropDownList dl = (DropDownList)(dgi.Cells[2].Controls[1]);
    if(dl.SelectedValue != null)
    {
      name+= dl.SelectedValue;
      name+="<BR>";
    }
  }
  Label2.Text = name;
}

Okay and that's it. After making your selection from the DropDownList, press button and it will bind your selection with a Label control on the WebForm.

In this code, we are using "DataGridItem" dgi whose purpose is to iterate through the DataGrid items. Next, we make a new DropDownList "dl" and place the value of the DropDownList which is in DataGrid cell [2] (cells start with 0) in it. We check using "if" condition that if the selected value is not null, we concatenate it in the string variable name and display name afterwards.

That's it! Happy coding :D

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.




I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions

 
QuestionMultiSelect DropdownList Using jQuery in Asp.Net MVC and C#.Net Pin
Member 123685433-Mar-16 18:43
Member 123685433-Mar-16 18:43 
Generalhelp Pin
piyush_hsp16-Mar-09 19:20
piyush_hsp16-Mar-09 19:20 
GeneralThank you Pin
Fastfootskater5-Jan-08 4:24
Fastfootskater5-Jan-08 4:24 
Generalits Really Good Article.. Pin
Virendrak3-Jun-07 22:42
Virendrak3-Jun-07 22:42 
its Really Good Article..

Never Think That You Have Failed Instead Always Think That u hav Better Chance Next Time...

Generalthank you Pin
zdlik20-Apr-07 8:59
zdlik20-Apr-07 8:59 
Generaldropdown Pin
sureshgganesh16-Apr-07 2:36
sureshgganesh16-Apr-07 2:36 
Generalvalue from dropdown Pin
zdlik6-Apr-07 10:47
zdlik6-Apr-07 10:47 
Newsgood artical Pin
ykgautam13-Dec-05 23:35
ykgautam13-Dec-05 23:35 
GeneralNeed help Pin
HamidTheProgrammer29-Aug-05 5:44
HamidTheProgrammer29-Aug-05 5:44 
GeneralThere is a small issue in this article Pin
shcolin2001@hotmail.com6-Jun-05 17:45
shcolin2001@hotmail.com6-Jun-05 17:45 
GeneralThank You Pin
Johan-BioVolt18-Aug-05 21:26
Johan-BioVolt18-Aug-05 21:26 
GeneralRe: Thank You Pin
azamsharp20-Aug-05 6:37
azamsharp20-Aug-05 6:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.