By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

DataView RowFilter Closest Date

P: n/a
I have a DataView (dv) that includes a StartDate DateTime column. I'd like to
return the DataView record that contains a StartDate value closest to my
IncidentDate value:

string filter = String.format("StartDate nearest {0}", IncidentDate);

dv.Rowfilter = filter;

Of course, there's no nearest function.

The IncidentDate could be anywhere in the range of StartDate values, so
simply sorting by the StartDate and taking the top record isn't good enough.

It appears that dv.Find() only accept objects. Otherwise that would work
pretty well.

Any suggestions?

Thanks,
--
Randy
Sep 11 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Randy,

You are going to have to do this on your own. You will have to cycle
through all the rows and then keep track of the value you are looking for
(whichever one is closest to your date).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"randy1200" <ra*******@newsgroups.nospamwrote in message
news:E1**********************************@microsof t.com...
>I have a DataView (dv) that includes a StartDate DateTime column. I'd like
to
return the DataView record that contains a StartDate value closest to my
IncidentDate value:

string filter = String.format("StartDate nearest {0}", IncidentDate);

dv.Rowfilter = filter;

Of course, there's no nearest function.

The IncidentDate could be anywhere in the range of StartDate values, so
simply sorting by the StartDate and taking the top record isn't good
enough.

It appears that dv.Find() only accept objects. Otherwise that would work
pretty well.

Any suggestions?

Thanks,
--
Randy

Sep 11 '06 #2

P: n/a
Hi Randy,

I agree with Nicholas. Since the expression column doesn't support
operators on DateTime, we have to go through each row and do the comparison
on our own.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Sep 12 '06 #3

P: n/a
Tom
Kevin,

I am trying to do something similar...I am getting lost a little on the
references to "expression column"...and their not supporting operators on
DateTime....I am not sure if that is in fact my issue...code sample
follows....

I am trying to simply apply a filter, two-of-three columns are date
types...My DataView.Count is zero after the filter is set....see code snip...

/**************
// 2) get the EventLogKey from the Event History table
DataView dvEventHist =
this.m_ds.Tables[TABLE_EVENT_HISTORY].DefaultView;

string filter = String.Format("TagName = '{0}' and DateTime =
#{1}# and DetectDateTime = #{2}#",
drv["Event"],
drv["EventTime"],
drv["DetectionTime"]);

dvEventHist.RowFilter = filter;

// QWERTY --- manually search the event history
bool found = false;
foreach (DataRow dr in this.m_ds.Tables[TABLE_EVENT_HISTORY].Rows)
{
if (dr["TagName"].Equals(drv["Event"]) &&
dr["DateTime"].Equals(drv["EventTime"]) &&
dr["DetectDateTime"].Equals(drv["DetectionTime"]))
{
found = true;
}

found = false;
}

// QWERTY -- try selecting from the datatable....also does not
work
DataRow[] selRows =
this.m_ds.Tables[TABLE_EVENT_HISTORY].Select(filter);
if (selRows.Length 0)
{
}
**************/

I do not understand why the DataView.RowFilter (and apparently also the
DataTable.Select(filter) ) do not make row matches...when cycling through the
rows manually I find what I expect...???

OK...so maybe that is what I have to do...I do not understand WHY...?

Thanks.
"Kevin Yu [MSFT]" wrote:
Hi Randy,

I agree with Nicholas. Since the expression column doesn't support
operators on DateTime, we have to go through each row and do the comparison
on our own.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.