Click here to Skip to main content
15,880,796 members
Articles / Web Development / ASP.NET
Tip/Trick

Different Items in RadCombobox in Each Row of RadGrid

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
24 Oct 2014CPOL 16.4K   5
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)


Written By
Netherlands Antilles Netherlands Antilles
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
majid torfi26-Oct-14 5:10
professionalmajid torfi26-Oct-14 5:10 
GeneralRe: My vote of 4 Pin
ATjong27-Oct-14 4:08
ATjong27-Oct-14 4:08 
QuestionRepeated query? Pin
Jeffrey T. Fritz24-Oct-14 2:35
sponsorJeffrey T. Fritz24-Oct-14 2:35 
You're connecting to and running your query for each row in the grid. Doesn't that seem like a significant amount of IO that you are adding to the server-side process of your page?

Can we improve the performance of your page by running the query once at the beginning of the page and during the ItemDataBound event apply a filter to a copy of the query results for each combobox that you want to load? I wonder what the performance difference is between your query strategy and a single query and filter strategy would be.
AnswerRe: Repeated query? Pin
ATjong25-Oct-14 7:34
ATjong25-Oct-14 7:34 
GeneralRe: Repeated query? Pin
ATjong27-Oct-14 6:18
ATjong27-Oct-14 6:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.