Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Different Items in RadCombobox in Each Row of RadGrid

4.00/5 (1 vote)
24 Oct 2014CPOL 17.2K  
Different items in RadCombobox in each row of RadGrid with sqlDataSource

Introduction

For a project, I use a RadComboBox in a templateColumn in a RadGrid to show some data. The combobox should show different items based on the value of a text field on the same row. In order to do this, I populate the sqlDatasource that is bound to the RadComboBox with all possible items and then hide the items I don't need in the combobox of each row.

Using the Code

To achieve this, use the ItemDataBound event of the RadGrid. This event gets fired for every row of the grid:

C#
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
RadComboBox ddl = (RadComboBox)item["Temp"].FindControl("RadComboBox2");
//GridDropDownColumn ddl2 = 
//(GridDropDownColumn)item["column"].FindControl("RadComboBox2");
string Name = item["Name"].Text;
int Row = 0; 
SqlDataSource4.ConnectionString = 
System.Configuration.ConfigurationManager.ConnectionStrings["ZISprodConnectionString2"].ConnectionString;
SqlDataSource4.SelectCommand = "SELECT RegNr,EQ_ID FROM EQUIPMENT WHERE NAME <> '" + Name + "'";
DataView dv = (DataView)SqlDataSource4.Select(DataSourceSelectArguments.Empty);
int TotalRows = dv.Count;
for (int i = 1; i <= TotalRows; i++)
{
DataRowView drv = dv[Row];
ddl.Items.Remove(ddl.Items.FindItemIndexByText(drv["RegNr"].ToString()));
Row = Row + 1;
}
}

As you can see, I use the value of the column "Name" to retrieve as the RegNr that are not of the current equipment item. I then loop through the combobox and hide each of the RegNr's that I selected. In this way, the combo everytime contains only the RegNr of the equipment item of that row.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)