Professional Documents
Culture Documents
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.
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.
Once you have done all these steps, your DataGrid should look something like this:
Populating DataGrid with Data:
Populating DataGrid is straight forward, just make the data source run a query and
bind the source to the DataGrid.
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.
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.
After this, your DropDownList will be populated with the data coming from
"Description" column of the NorthWind database.
Remember that we added a HTML Button control. In the Click event of the Button,
write this code:
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.