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:
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");
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.