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.
Add DropDownList to DataGrid:
Okay adding dropdownlist to datagrid is straight forward. 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 Column and One Template Column (Remember To Add Bound Column First and Than Template Column). Uncheck Create Column 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 windows 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 ITS 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 in The Last Add ONE ASP Button Control Anywhere on The Form.
Once you have done all these Steps your database:
Populating DataGrid with data:
Populating DataGrid is Straight Forward Just Make The Datasource Run a Query and Bind The Source To The DataGrid.
Private Void Page_Load (Object Sender, System.EventArgs E)
{
IF (! page.ispostback) {
Binddata ();
Populationelist ();
}
}
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 YOUERY LIKE this and always use stored procedures for security purposes.
Populating DropdownList from Database:
You can always populate your dropdownload by useing it.................................
Public Dataset Populates () {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 that the method "PopulateList" is returning DataSet. This dataset is used by the DropDownList to populate itself. Let's see how DropDownList named "ddlList" use 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 "description" Column of Northwind Database.
Here is a picture of the completion Webform:
Button Click Event Code:
Remember that we added an asp button control In the onclick event of the button write this code: 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 = "
";}} label2.text = name; }
.
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 starts with 0) IN it. we check using "if" condition tring value is not null if the successd value is not null we consted value is in the string variable name and display name atway.
Thats it! Happy Coding: D