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 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 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 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 datagrid should look something like this:

Sample screenshot

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(); 

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 comming from the database column.

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 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.

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

After this your DropDownList will be populated with the data comming from "Description" Column of NorthWind Database.

Here is a picture of the complete webform:

Sample screenshot

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+="<BR>";

}

}

Label2.Text = name;

}

Okay and thats it after making your selection from all 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 starts with 0) in it. We check using "if" condition that it the selected value is not null if the selected value is not null we concatenate it in the string variable name and display name afterwards.

Thats it! happy coding :D

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值