DropDown List - Default Item - Two RowFilters possible?

H

Harry

Hi,

I have a problem with the default Item of an asp:dropdownlist.

I databind the dropdown list in the code with values from a SQL
Database as per below.

ViewOptions.RowFilter = "Code LIKE 'MM%'";
ddlRam.DataSource = ViewOptions;
ddlRam.DataTextField = "Name";
ddlRam.DataValueField = "IncPrice";
ddlRam.DataBind();

But what I would like now, is for the default option to be set to a
different value based on the below filter:
ViewOptions.RowFilter = "PrRange LIKE 'S' AND Code Like 'MM%'";


Can anyone tell me if this is actually possible.
(Ideally something like the below line would be perfect, if it
worked!!)
ddlRam.SelectedItem = ViewOptions.RowFilter = "PrRange LIKE 'S' AND
Code Like 'MM%'";


ANy help appriciated as always.

Thanks
H
 
F

Fred Hirschfeld

You cannot do this like you have indicated, but after the bind you could
look through your DataSet (after your selection filter) and use the
..Selected = true of the items in the ddlRam dropdown that match the filtered
results.

You could also package this up as a new function on a derived dropdownlist
to you could use a filter clause for selected items...

Fred
 
H

Harry Singh

Thans for the reply Fred.
Is there any chance you could provide more details on your proposed
solution.
(Link to sample would be fine, and then i'll try to work ou for myself!)

I don't really understand how I would put your suggestions into
practice!

Many thanks
H
 
F

Fred Hirschfeld

I don't actually have some specific, but I will try some directly here (not
compiled...). Hope this gets you closer to what you are looking for... This
may not be optimal in terms of performance but should demonstrate what I was
indicating:


public class FilterSelectDropDownList : DropDownList {
private string m_FilterCriteria = string.Empty;

public FilterSelectDropDownList() : base() {
}

public string FilterCriteria {
get {
return m_FilterCriteria;
}
set {
m_FilterCriteria = value;

// if there is bound data, apply the select filter now.
ApplyFilter();
}
}

public override void DataBind() {
base.DataBind();

ApplyFilter();
}

protected virtual void ApplyFilter() {
if (m_FilterCriteria != string.Empty && DataSource != null &&
DataSource is DataView) {
DataView dv = DataSource as DataView;

string oldFilter = dv.RowFilter;
dv.RowFilter = m_FilterCriteria;

foreach(DataViewRow dvr in dv) {
object valueVal = dvr.Row[this.DataValueField];

foreach(ListItem item in this.Items) {
if (item.Value.Equals(valueVal)) {
item.Selected = true;
}
}
}

dv.RowFilter = oldFilter;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,262
Latest member
EffiePju4

Latest Threads

Top