Videos | Podcasts

DropDownList Inside The GridView Method 2
AzamSharp
Published Date: 4/1/2006 6:24:15 PM
Views: 18785

Abstract:
This is the second installment of the article DropDownList Inside the GridView. In the previous article DropDownList Inside the GridView I talked about how you can populate the DropDownList inside the GridView with different data depending on the row of the article using a DataSet. In this article I will show demonstrate that how you can achieve the same behavior using Entity Classes.

Introduction:

This is the second installment of the article, DropDownList Inside the GridView. In the previous article DropDownList Inside the GridView I talked about how you can populate the DropDownList inside the GridView with different data using a DataSet. In this article I will demonstrate that how you can achieve the same behavior using Entity Classes.

Analysis:

The first thing that you need to do is to create the Category and Product classes. Take a look at both classes below:

public class Product

{

private int _productID;

private string _productName;

public int ProductID

{

get { return _productID; }

set { _productID = value; }

}

public string ProductName

{

get { return _productName; }

set { _productName = value; }

}

public Product(int productID, string productName)

{

_productID = productID;

_productName = productName;

}

public Product()

{

}

 

Since, a single category can have multiple products that is why I am going to declare a list of Product inside the Category class. Take a look at the Category class to have better idea.

public class Category

{

private int _categoryID;

private string _categoryName;

private List<Product> _productList = new List<Product>();

public int CategoryID

{

get

{

return _categoryID;

}

set { _categoryID = value; }

}

public string CategoryName

{

get { return _categoryName; }

set { _categoryName = value; }

}

public List<Product> ProductList

{

get { return _productList; }

set { _productList = value; }

}

public Category(int categoryID, string categoryName)

{

_categoryID = categoryID;

_categoryName = categoryName;

}

public Category() { }

}

 

After creating the required classes we need to create some dummy data which will be used to populate the DropDownLists. In the code below I demonstrated that how you can create dummy categories and populate dummy products for the particular category. 

private List<Category> categoryList = new List<Category>();

private void FillList()

{

for (int j = 1; j <= 10; j++)

{

Category category = new Category(j,"Category"+j);

for (int i = 1; i <= 10; i++)

{

category.ProductList.Add(new Product(i, "ProductA" + j + i));

}

categoryList.Add(category);

}

 

// Bind the list to the GridView

GridView1.DataSource = categoryList;

GridView1.DataBind();

}

 

The FillList() method above populates the Category class with dummy categories and products. Now, the only thing left to do is to fill the dummy data into the DropDownList. Take a look at the code below:

private int _counter = 0;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");

ddl.DataSource = categoryList[_counter].ProductList;

ddl.DataTextField = "ProductName";

ddl.DataBind();

// increment the counter

_counter++;

}

}

The code above is implemented in the GridView_RowDataBound event, which is fired every time a row is bound on the GridView. The heart of this application is the line ddl.DataSource = categoryList[_counter].ProductList; which retrieves the product list of a particular category and assign it to the DataSource property of the DropDownList control. After the product list is assigned to the data source I simply assign the ProductName property to the DataTextField property of the DropDownList.

Take a look at the image below to have a clear idea what the GridView looks like at runtime.

I hope you like article, happy coding!

 

 

 




Did you like this article?
kick it on DotNetKicks.com Submit
Similar Articles

Solving CheckAll Problems for GridView with CheckBoxes in Multiple Columns

Solving Multiple GridViews CheckBox Selection Problem

Exporting GridView to PDF Document

Nested GridView with TreeView Like Structure

Updating Only Changed Rows in the GridView Control

Enter Comment/Feedback

 
 
 
 
 

Comments/Feedbacks