How do I refresh DataView filter efficiently

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The following code retrieves data into a dataset, and then creates a dataview
with a filter. This dataview is then attached to a combobox. When the
effective date changes, I would like to see the newly filtered data without
having to create a new dataview each time. Is that possible?
Also, do I need to attach the dataview to the combo box each time?

if (AgencyCounties == null || sPropertyState.Text != prevPropertyState)
{
AgencyCounties = Producer.GetAgencyCounties(Producer.AgencyId,
sPropertyState.Text,"11");

AgencyCounties.Tables["Table"].TableName = "AgencyCounty";
prevPropertyState = sPropertyState.Text;
}

sPropertyCountyCd.BeginUpdate();

DataView viewCounties = new DataView(AgencyCounties.Tables["AgencyCounty"]);

string Filter = "StartDt <= '" + sEffectiveDt.Text + "' AND (StopDt is Null
OR StopDt > '" + sEffectiveDt.Text + "')";

viewCounties.RowFilter = Filter;

sPropertyCountyCd.DisplayMember = "Name";
sPropertyCountyCd.ValueMember = "CountyCd";
sPropertyCountyCd.DataSource = viewCounties;
sPropertyCountyCd.EndUpdate();
 
Vern,

You should be able to set the Filter of the view that the list is bound
to, and it should change the list automatically when the filter is set.

Hope this helps.
 
I changed it to this, and it doesn't change what the combobox is showing.
Debug shows it executing "viewCounties.RowFilter = Filter" but the drop down
list doesn't change. What am I doing wrong?

// Refresh the Dataset if the Property State changes
if (AgencyCounties == null || sPropertyState.Text != prevPropertyState)
{
AgencyCounties = Producer.GetAgencyCounties(Producer.AgencyId,
sPropertyState.Text,"11");
AgencyCounties.Tables["Table"].TableName = "AgencyCounty";
prevPropertyState = sPropertyState.Text;
}



if (viewCounties == null)
{
sPropertyCountyCd.BeginUpdate();
viewCounties = new DataView(AgencyCounties.Tables["AgencyCounty"]);
Filter = "StartDt <= '" + sEffectiveDt.Text + "' AND (StopDt is Null OR
StopDt > '" + sEffectiveDt.Text + "')";
viewCounties.RowFilter = Filter;
sPropertyCountyCd.DisplayMember = "Name";
sPropertyCountyCd.ValueMember = "CountyCd";
sPropertyCountyCd.DataSource = viewCounties;
sPropertyCountyCd.EndUpdate();
}
else
{
viewCounties.RowFilter = Filter;
}

Nicholas Paldino said:
Vern,

You should be able to set the Filter of the view that the list is bound
to, and it should change the list automatically when the filter is set.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vern said:
The following code retrieves data into a dataset, and then creates a
dataview
with a filter. This dataview is then attached to a combobox. When the
effective date changes, I would like to see the newly filtered data
without
having to create a new dataview each time. Is that possible?
Also, do I need to attach the dataview to the combo box each time?

if (AgencyCounties == null || sPropertyState.Text != prevPropertyState)
{
AgencyCounties = Producer.GetAgencyCounties(Producer.AgencyId,
sPropertyState.Text,"11");

AgencyCounties.Tables["Table"].TableName = "AgencyCounty";
prevPropertyState = sPropertyState.Text;
}

sPropertyCountyCd.BeginUpdate();

DataView viewCounties = new
DataView(AgencyCounties.Tables["AgencyCounty"]);

string Filter = "StartDt <= '" + sEffectiveDt.Text + "' AND (StopDt is
Null
OR StopDt > '" + sEffectiveDt.Text + "')";

viewCounties.RowFilter = Filter;

sPropertyCountyCd.DisplayMember = "Name";
sPropertyCountyCd.ValueMember = "CountyCd";
sPropertyCountyCd.DataSource = viewCounties;
sPropertyCountyCd.EndUpdate();
 
Never mind. I just realized my mistake. I wasn't changing the filter string.
The else should have been as follows:
else
{
Filter = "StartDt <= '" + sEffectiveDt.Text + "' AND (StopDt is Null OR
StopDt > '" + sEffectiveDt.Text + "')";
viewCounties.RowFilter = Filter;
}

Vern said:
I changed it to this, and it doesn't change what the combobox is showing.
Debug shows it executing "viewCounties.RowFilter = Filter" but the drop down
list doesn't change. What am I doing wrong?

// Refresh the Dataset if the Property State changes
if (AgencyCounties == null || sPropertyState.Text != prevPropertyState)
{
AgencyCounties = Producer.GetAgencyCounties(Producer.AgencyId,
sPropertyState.Text,"11");
AgencyCounties.Tables["Table"].TableName = "AgencyCounty";
prevPropertyState = sPropertyState.Text;
}



if (viewCounties == null)
{
sPropertyCountyCd.BeginUpdate();
viewCounties = new DataView(AgencyCounties.Tables["AgencyCounty"]);
Filter = "StartDt <= '" + sEffectiveDt.Text + "' AND (StopDt is Null OR
StopDt > '" + sEffectiveDt.Text + "')";
viewCounties.RowFilter = Filter;
sPropertyCountyCd.DisplayMember = "Name";
sPropertyCountyCd.ValueMember = "CountyCd";
sPropertyCountyCd.DataSource = viewCounties;
sPropertyCountyCd.EndUpdate();
}
else
{
viewCounties.RowFilter = Filter;
}

Nicholas Paldino said:
Vern,

You should be able to set the Filter of the view that the list is bound
to, and it should change the list automatically when the filter is set.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vern said:
The following code retrieves data into a dataset, and then creates a
dataview
with a filter. This dataview is then attached to a combobox. When the
effective date changes, I would like to see the newly filtered data
without
having to create a new dataview each time. Is that possible?
Also, do I need to attach the dataview to the combo box each time?

if (AgencyCounties == null || sPropertyState.Text != prevPropertyState)
{
AgencyCounties = Producer.GetAgencyCounties(Producer.AgencyId,
sPropertyState.Text,"11");

AgencyCounties.Tables["Table"].TableName = "AgencyCounty";
prevPropertyState = sPropertyState.Text;
}

sPropertyCountyCd.BeginUpdate();

DataView viewCounties = new
DataView(AgencyCounties.Tables["AgencyCounty"]);

string Filter = "StartDt <= '" + sEffectiveDt.Text + "' AND (StopDt is
Null
OR StopDt > '" + sEffectiveDt.Text + "')";

viewCounties.RowFilter = Filter;

sPropertyCountyCd.DisplayMember = "Name";
sPropertyCountyCd.ValueMember = "CountyCd";
sPropertyCountyCd.DataSource = viewCounties;
sPropertyCountyCd.EndUpdate();
 
Back
Top